Vector Recycling in R
What is Vector Recycling?
Vector recycling is the process by which each of the vectors in a group of vectors is standardized to have the same length 1. More precisely, each vector is augmented to be as long as the longest vector in the group. This is accomplished by “recycling” (repeating) elements from the front of each vector until that vector reaches the target length - wrapping around as many times as needed.
What does it do?
Vector recycling ensures that each vector in a group of vectors has the same number of elements. Typically, vector recycling happens with functions that operate element-wise on their inputs, especially those that take a variable number of input vectors.
How does it work?
To recap, vector recycling “recycles” elements from the front of a vector until that vector reaches the target length (the length of the largest vector of the group), wrapping around as many times as needed.
Again, the purpose of vector recycling is to ensure that every vector in a group has the same number of elements (or length).
The following example shows the result of vector recycling performed during a call to the paste()
function. From the official R Documentation:
paste
converts its arguments (viaas.character
) to character strings, and concatenates them (separating them by the string given bysep
). If the arguments are vectors, they are concatenated term-by-term to give a character vector result. Vector arguments are recycled as needed, with zero-length arguments being recycled to ““. (emphasis mine)
# An example of vector recycling
foo <- c("i", "ii", "iii", "iv", "v", "vi", "vii") # `foo` has 7 elements
bar <- c("A", "B", "C") # `bar` has 3 elements
baz <- c("North", "South", "East", "West") # `baz has 4 elements`
# paste() constructs string concatenations element-wise. The `sep` argument
# determines the concacetantion character.
#
# The above code should produce a string with underscores ("_") separating
# elements at the same position across the vectors.
#
# Since `bar` and `baz` are both shorter than `foo`, they - their elements - will be recycled.
#
# Recycled Versions:
# bar <- c("A", "B", "C", "A", "B", "C", "A")
# baz <- c("North", "South", "East", "West", "North", "South", "East")
> x <- paste(foo, bar, baz, sep = "_")
> cat(x, sep = "\n")
i_A_North
ii_B_South
iii_C_East
iv_A_West # Bar repeats
v_B_North # Baz repeats
vi_C_South
vii_A_East # Bar repeats, again
The example above uses paste()
to concatenate three vectors: foo
, bar
, and baz
. Foo
is the longest of the group with seven elements. Bar
has 3 elements and baz
has 4. Accordingly, the elements of bar
and baz
will be recycled until they are 7 elements long too. 2
How can I use it?
Vector recycling makes it easier to describe/express repeating, cyclical patterns among a set of vectors.
As an example, suppose that I have n items that I want to divide, round-robin, among k groups. Vector recycling makes this easy to do.
The following example shows how vector recycling can be used to assign 10 elements to four different groups (“A”, “B”, “C”, and “D”).
> x <- 1:10
>
> result <- mapply(function(a, b) sprintf("%d is in group %s", a, b), x, c("A","B","C","D"))
Warning message:
In mapply(function(a, b) sprintf("%d is in group %s", a, b), x, :
longer argument not a multiple of length of shorter
> cat(result, sep = "\n")
1 is in group A
2 is in group B
3 is in group C
4 is in group D
5 is in group A
6 is in group B
7 is in group C
8 is in group D
9 is in group A
10 is in group B