Vector Recycling

Vector recycling in R is one of the useful concept. If we try to perform some operation on two vectors having different length then R automatically recycles the required number of additional vectors.

# initialization
(a <- 1:10)
##  [1]  1  2  3  4  5  6  7  8  9 10
(b <- 1:5)
## [1] 1 2 3 4 5
(a+b)
##  [1]  2  4  6  8 10  7  9 11 13 15
Here, in the above example the shorter vector is recycled and added to match the number of operators. Since the sum between these number a and b is element wise the remaning 5 entries are automatically recycled. However, if the longer object is not multiple of shorter object the interpreter warns us about the situation but still performs the calculation.
(c <- 1:4)
## [1] 1 2 3 4
(a*c)
## Warning in a * c: longer object length is not a multiple of shorter object
## length
##  [1]  1  4  9 16  5 12 21 32  9 20

So this is the concept of vector recycling in R.