37  Vector and matrix operation

From math symbols to syntax

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 3
    vec <- 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 Inf
    10 / 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 values
    mat <- 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 vectors
    vec + new_vec
    [1] 12 20 31
    # Multiply corresponding elements of the two vectors
    vec * 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 purchased
    fruit_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 matrix
    fruit_quantity
         [,1] [,2] [,3]
    [1,]    6    3  0.5
    [2,]    3    1  0.0
    [3,]    0   10  0.0
    [4,]    1    0  2.0
    # Define a 3x2 matrix representing fruit prices at two different shops
    price_shops <- matrix(
      data = c(500, 1000, 2000, 600, 800, 2500),
      nrow = 3
    )
    
    # Display the price matrix
    price_shops
         [,1] [,2]
    [1,]  500  600
    [2,] 1000  800
    [3,] 2000 2500
    # Perform matrix multiplication (%*%) to compute total expenditure per person per shop
    fruit_quantity %*% price_shops
          [,1] [,2]
    [1,]  7000 7250
    [2,]  2500 2600
    [3,] 10000 8000
    [4,]  4500 5600

Tips and warnings

  • 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

    # A 2 x 2 diagonal matrix
    diag(c(12, 37))
         [,1] [,2]
    [1,]   12    0
    [2,]    0   37
    # A 4 x 4 identity matrix
    diag(4)
         [,1] [,2] [,3] [,4]
    [1,]    1    0    0    0
    [2,]    0    1    0    0
    [3,]    0    0    1    0
    [4,]    0    0    0    1
  • 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 vector
    another_vec <- c(1, 10, 100)
    
    # Apply the sine function element-wise
    sin(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.


  1. Not all functions are vectorized↩︎