17  Vectorized Operations

Many built-in R functions are vectorized and so are many functions from external packages as well.

A vectorized function operates on all elements of an object.

Vectorization is very efficient: it can save both human (your) time and machine time.

In many cases, applying a function on all elements simultaneously may seem like the obvious or expected behavior, but since not all functions are vectorized, make sure to check the documentation (and/or test whether a function is vectorized using a simple example).

17.1 Operations between vectors of equal length

Such operations are applied between corresponding elements of each vector:

x <- 1:10
z <- 11:20
x + z
 [1] 12 14 16 18 20 22 24 26 28 30

i.e. the above is equal to c(x[1] + z[1], x[2] + z[2], ..., x[n] + z[n]).

Weight <- rnorm(20, mean = 80, sd = 1.7)
Weight
 [1] 78.38965 78.23646 80.14821 81.54436 81.56161 81.96843 81.52541 79.49612
 [9] 78.35537 75.92439 80.08871 81.82717 78.64154 79.87043 83.24592 78.19457
[17] 80.25587 79.34153 80.90151 79.58486
Height <- rnorm(20, mean = 1.7, sd = 0.1)
Height
 [1] 1.696230 1.781965 1.627933 1.614587 1.845924 1.811732 1.599953 1.663111
 [9] 1.643155 1.781417 1.588865 1.507473 1.864233 1.821707 1.618385 1.582522
[17] 1.755496 1.828304 1.585851 1.674656
BMI <- Weight/Height^2
BMI
 [1] 27.24515 24.63830 30.24272 31.28030 23.93636 24.97232 31.84775 28.74111
 [9] 29.02097 23.92490 31.72467 36.00795 22.62828 24.06739 31.78332 31.22317
[17] 26.04216 23.73580 32.16857 28.37785

17.2 Operations between a vector and a scalar

In this cases, the scalar is repeated to match the length of the vector, i.e. it is recycled:

x + 10
 [1] 11 12 13 14 15 16 17 18 19 20
x * 2
 [1]  2  4  6  8 10 12 14 16 18 20
x / 10
 [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
x ^ 2
 [1]   1   4   9  16  25  36  49  64  81 100

17.3 Operations between vectors of unequal length: value recycling

Operations between a vector and a scalar are a special case of operations between vectors of unequal length. Whenever you perform an operation between two objects of different length, the shorter object’s elements are recycled:

x + c(2:1)
 [1]  3  3  5  5  7  7  9  9 11 11

Operations between objects of unequal length can occur by mistake. If the shorter object’s length is a multiple of the longer object’s length, there will be no error or warning, as above. Otherwise, there is a warning (which may be confusing at first) BUT recycling still happens and is highly unlikely to be intentional.

x + c(1, 3, 9)
Warning in x + c(1, 3, 9): longer object length is not a multiple of shorter
object length
 [1]  2  5 12  5  8 15  8 11 18 11

17.4 Vectorized matrix operations

Operations between matrices are similarly vectorized, i.e. performed between corresponding elements:

a <- matrix(1:4, nrow = 2)
b <- matrix(11:14, nrow = 2)
a
     [,1] [,2]
[1,]    1    3
[2,]    2    4
b
     [,1] [,2]
[1,]   11   13
[2,]   12   14
a + b
     [,1] [,2]
[1,]   12   16
[2,]   14   18
a * b
     [,1] [,2]
[1,]   11   39
[2,]   24   56
a / b
           [,1]      [,2]
[1,] 0.09090909 0.2307692
[2,] 0.16666667 0.2857143

17.5 Vectorized functions

Some examples of common mathematical operations that are vectorized:

log(x)
 [1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101
 [8] 2.0794415 2.1972246 2.3025851
sqrt(x)
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427
 [9] 3.000000 3.162278
sin(x)
 [1]  0.8414710  0.9092974  0.1411200 -0.7568025 -0.9589243 -0.2794155
 [7]  0.6569866  0.9893582  0.4121185 -0.5440211
cos(x)
 [1]  0.5403023 -0.4161468 -0.9899925 -0.6536436  0.2836622  0.9601703
 [7]  0.7539023 -0.1455000 -0.9111303 -0.8390715