r-novice-gapminder
r-novice-gapminder copied to clipboard
Suggestion: vectorisation with unequal length vectors
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
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?
This was addressed in #822.
Thank you @andreaczhang and @chi-raag for your contributions!