The sole purpose of this chapter is to introduce the symbols and syntax for matrix operations (element-wise arithmetic operations and matrix algebra operations) in R. This implements the mathematical concepts presented in Chapter 6 and Chapter 7.
Formal
Scalar operation: when combining a number and a vector or a matrix, the same symbol notation addition +, subtraction -, multiplication *, division /.This is the same as in Chapter 6.
2*c(a, b) =c(2* a, 2* b)
Element-wise operation: when two objects are the same dimensions (vector of same length or matrix of same dimensions), the same symbols are used.
c(a, b) *c(c, d) =c(a * c, b * d)
Matrix multiplication (as in Chapter 7): the R implementation symbol is %*%. The star is to remember that it is still a multiplication. Unlike the arithmetic operation which is the same in any langauge, this syntax is specific to R.
Examples
Scalar operations
# Define a numeric vector of length 3vec <-c(2, 0, 1)# Multiply each element in the vector by a scalar (-2)-2* vec
[1] -4 0 -2
# Divide a scalar (10) by each vector element; division by zero yields Inf10/ vec
[1] 5 Inf 10
# Divide each element in the vector by a scalar (10)vec /10
[1] 0.2 0.0 0.1
# Create a 2x3 matrix using vector valuesmat <-matrix(c(2, 3, 4, vec), nrow =2)# Multiply all entries in the matrix by a scalar (-2)-2* mat
[,1] [,2] [,3]
[1,] -4 -8 0
[2,] -6 -4 -2
Element-wise operations between vectors of equal length
# Define a second numeric vector of equal length (3)new_vec <-c(10, 20, 30)# Add corresponding elements of the two vectorsvec + new_vec
[1] 12 20 31
# Multiply corresponding elements of the two vectorsvec * new_vec
[1] 20 0 30
Matrix multiplication to calculate total expenditure per person per shop Chapter 7
# Define a 4x3 matrix representing quantities of fruits purchasedfruit_quantity <-matrix(data =c(6, 3, 0, 1, 3, 1, 10, 0, 0.5, 0, 0, 2),nrow =4,ncol =3)# Display the fruit quantity matrixfruit_quantity
# Define a 3x2 matrix representing fruit prices at two different shopsprice_shops <-matrix(data =c(500, 1000, 2000, 600, 800, 2500),nrow =3)# Display the price matrixprice_shops
A diagonal matrix (where all elements outside the main diagonal are zero) can be created using diag(vec), where vec specifies the values along the diagonal. A useful shortcut is diag(m); when given a single integer m, it creates an identity matrix with ones along the main diagonal
The standard mathematical order of operations, parentheses, and rules apply. In matrix multiplication, the number of columns in the first matrix must equal the number of rows in the second matrix, and matrix division is undefined Chapter 7.
R is vectorized, meaning that when the argument is a vector, a function operates on each element individually1.
# Define a numeric vectoranother_vec <-c(1, 10, 100)# Apply the sine function element-wisesin(another_vec)
[1] 0.8414710 -0.5440211 -0.5063656
A unexpected behavior in R:
c(3, 2 , 1) +c(1, 2, 3, 4, 5)
Warning in c(3, 2, 1) + c(1, 2, 3, 4, 5): longer object length is not a
multiple of shorter object length
[1] 4 4 4 7 7
R still performs addition even though the two vectors are not of equal length, although it issues a warning. Such an operation is not defined in standard mathematics.