Fiveable

💻Advanced R Programming Unit 2 Review

QR code for Advanced R Programming practice questions

2.1 Vectors and matrices

💻Advanced R Programming
Unit 2 Review

2.1 Vectors and matrices

Written by the Fiveable Content Team • Last updated September 2025
Written by the Fiveable Content Team • Last updated September 2025
💻Advanced R Programming
Unit & Topic Study Guides

Vectors and matrices are fundamental data structures in R, forming the building blocks for more complex data manipulation. They allow you to store and organize data efficiently, whether it's simple numeric values or complex multidimensional arrays.

Understanding how to create, manipulate, and perform operations on vectors and matrices is crucial. These skills set the foundation for working with larger datasets, performing statistical analyses, and creating visualizations in R. Mastering these concepts opens doors to more advanced data handling techniques.

Vectors and Matrices in R

Creating and Manipulating Vectors

  • Create numeric vectors using the c() function, which combines arguments into a vector
    • Example: numeric_vector <- c(1, 2, 3, 4, 5)
  • Create character vectors by enclosing elements in quotes
    • Example: character_vector <- c("a", "b", "c", "d")
  • Index vectors using square brackets [ ] to select specific elements
    • Example: numeric_vector[1] returns the first element
  • Use negative indexing to exclude elements
    • Example: numeric_vector[-3] returns the vector without the third element
  • Assign names to vector elements for more intuitive indexing
    • Example: names(numeric_vector) <- c("one", "two", "three", "four", "five")

Creating and Manipulating Matrices

  • Create matrices using the matrix() function, specifying the data, number of rows, and number of columns
    • Example: matrix(1:6, nrow = 2, ncol = 3)
  • Arrange matrix elements sequentially by column
  • Combine vectors using cbind() or rbind() to create matrices by columns or rows
    • Example: cbind(1:3, 4:6) creates a matrix with two columns
  • Index matrices using square brackets [ ] to select specific elements, rows, or columns
    • Example: matrix[1, 2] returns the element in the first row and second column
  • Assign names to matrix rows and columns for more intuitive indexing
    • Example: rownames(matrix) <- c("row1", "row2")

Atomic vs Recursive Vectors

Atomic Vectors

  • Understand the six types of atomic vectors: logical, integer, double, character, complex, and raw
  • Recognize that atomic vectors are homogeneous, requiring all elements to be the same data type
  • Use comparison operators (<, >, <=, >=, ==, !=) to compare values in atomic vectors, returning logical vectors
    • Example: numeric_vector > 3 returns a logical vector indicating which elements are greater than 3
  • Work with factors, a special type of atomic vector for categorical data, created using factor()
    • Example: factor(c("a", "b", "a", "c")) creates a factor with three levels

Recursive Vectors (Lists and Data Frames)

  • Create lists using the list() function to combine elements of different data types
    • Example: list(1, "a", TRUE) creates a list with numeric, character, and logical elements
  • Recognize that lists are heterogeneous and can contain other lists
  • Work with data frames, a special type of list where each element is an atomic vector of the same length
    • Example: data.frame(x = 1:3, y = c("a", "b", "c")) creates a data frame with two columns
  • Understand that data frames are two-dimensional and resemble matrices, but can have different data types in each column

Operations on Vectors and Matrices

Mathematical Operations

  • Use basic arithmetic operators (+, -, ``, /, ^) for element-wise operations on vectors
    • Example: numeric_vector + 1 adds 1 to each element of the vector
  • Perform element-wise addition and subtraction on matrices
  • Use * for element-wise multiplication and %*% for matrix multiplication
    • Example: matrix1 matrix2 performs element-wise multiplication
  • Compute sums, means, minimums, maximums, and products using sum(), mean(), min(), max(), and prod() functions
    • Example: sum(numeric_vector) calculates the sum of all elements in the vector
  • Calculate row and column sums and means using rowSums(), rowMeans(), colSums(), and colMeans() functions
    • Example: rowSums(matrix) computes the sum of each row in the matrix

Handling Missing Values

  • Understand that missing values (NA) have special behavior in mathematical operations
  • Recognize that most operations involving NA will return NA as the result
  • Use the na.rm argument set to TRUE to remove missing values from calculations
    • Example: mean(numeric_vector, na.rm = TRUE) calculates the mean excluding missing values

Applying Functions to Data Structures

Applying Functions to Matrices

  • Use the apply() function to apply a function to the rows or columns of a matrix
    • Example: apply(matrix, 1, sum) applies the sum() function to each row of the matrix
  • Specify 1 for rows or 2 for columns as the second argument in apply()

Applying Functions to Lists and Vectors

  • Apply functions to each element of a list or vector using lapply(), returning a list of the same length as the input
    • Example: lapply(list, sqrt) applies the square root function to each element of the list
  • Simplify the output of lapply() to a vector or matrix using sapply() when possible
    • Example: sapply(numeric_vector, sqrt) applies the square root function and returns a numeric vector
  • Use tapply() to apply a function to subsets of a vector based on a grouping factor
    • Example: tapply(vector, group_factor, mean) calculates the mean for each group defined by the factor
  • Apply functions to corresponding elements of multiple lists or vectors using mapply()
    • Example: mapply(sum, list1, list2) applies the sum() function to the corresponding elements of list1 and list2