r-novice-gapminder icon indicating copy to clipboard operation
r-novice-gapminder copied to clipboard

Suggestion: vectorisation with unequal length vectors

Open andreaczhang opened this issue 2 years ago • 1 comments

This suggestion is related to the following episode: https://swcarpentry.github.io/r-novice-gapminder/09-vectorization/index.html

In the current version of this chapter, vector addition/multiplication is only for

  • vector with a single value (scalar)
  • vector with another vector of the same length

Might be useful to include an example of addition with unequal length vectors, so that the users can understand the behavior of R vectorisation.

a <- c(1, 2, 3)
b <- c(4, 5)
a+b

andreaczhang avatar Jul 12 '22 13:07 andreaczhang

Thank you for your suggestion @andreaczhang! This looks like a good point to tell new R users about. In my experience, "silent" recycling of vector values (rather than always throwing a warning or an error) can sometimes surprise people who are new to R.

Maybe this point could be addressed in a concise "Tip" box towards the end of the lesson? For example, one could present both a case throwing a warning longer object length is not a multiple of shorter object length and a case where the recycling of the shortest vector is entirely silent. Or maybe you already had something else in mind?

matthieu-bruneaux avatar Jul 18 '22 00:07 matthieu-bruneaux

This was addressed in #822.

Thank you @andreaczhang and @chi-raag for your contributions!

matthieu-bruneaux avatar Apr 14 '23 14:04 matthieu-bruneaux