x <- 1:4
x * 2Vectorization
Adapted from Software Carpentry
Overview
Questions:
- How can I operate on all the elements of a vector at once?
Objectives:
- To understand vectorized operations in R
Introduction
Most of R’s functions are vectorized, meaning that the function will operate on all elements of a vector without needing to loop through and act on each element one at a time. This makes writing code more concise, easy to read, and less error prone.
[1] 2 4 6 8
The multiplication happened to each element of the vector.
We can also add two vectors together:
y <- 6:9
x + y[1] 7 9 11 13
Each element of x was added to its corresponding element of y:
x: 1 2 3 4
+ + + +
y: 6 7 8 9
---------------
7 9 11 13
Vectorized Operations vs. Loops
Here is how we would add two vectors together using a for loop:
output_vector <- c()
for (i in 1:4) {
output_vector[i] <- x[i] + y[i]
}
output_vector[1] 7 9 11 13
Compare this to the output using vectorised operations:
sum_xy <- x + y
sum_xy[1] 7 9 11 13
The vectorized version is much more concise and easier to read!
Vectorized Operations with Comparisons and Logic
Comparison operators, logical operators, and many functions are also vectorized:
Comparison operators:
x > 2[1] FALSE FALSE TRUE TRUE
Logical operators:
a <- x > 3 # or, for clarity, a <- (x > 3)
a[1] FALSE FALSE FALSE TRUE
any() will return TRUE if any element of a vector is TRUE.
all() will return TRUE if all elements of a vector are TRUE.
Vectorized Functions
Most functions also operate element-wise on vectors:
x <- 1:4
log(x)[1] 0.0000000 0.6931472 1.0986123 1.3862944
Vectorized Operations on Matrices
Vectorized operations work element-wise on matrices:
m <- matrix(1:12, nrow = 3, ncol = 4)
m * -1 [,1] [,2] [,3] [,4]
[1,] -1 -4 -7 -10
[2,] -2 -5 -8 -11
[3,] -3 -6 -9 -12
Very important: the operator * gives you element-wise multiplication!
To do matrix multiplication, we need to use the %*% operator:
m %*% matrix(1, nrow = 4, ncol = 1) [,1]
[1,] 22
[2,] 26
[3,] 30
matrix(1:4, nrow = 1) %*% matrix(1:4, ncol = 1) [,1]
[1,] 30
For more on matrix algebra, see the Quick-R reference guide.
Recycling: Operations on Vectors of Unequal Length
Operations can also be performed on vectors of unequal length, through a process known as recycling. This process automatically repeats the smaller vector until it matches the length of the larger vector. R will provide a warning if the larger vector is not a multiple of the smaller vector.
x <- c(1, 2, 3)
y <- c(1, 2, 3, 4, 5, 6, 7)
x + yWarning in x + y: longer object length is not a multiple of shorter object
length
[1] 2 4 6 5 7 9 8
Vector x was recycled to match the length of vector y:
x: 1 2 3 1 2 3 1
+ + + + + + +
y: 1 2 3 4 5 6 7
-----------------------
2 4 6 5 7 9 8
Key Points
- Use vectorized operations instead of loops
- Vectorized code is more concise, easier to read, and less error-prone
- Most R functions operate element-wise on vectors
- Be aware of vector recycling when operating on vectors of different lengths
- Use
%*%for matrix multiplication, not*