Matrix Indexing in R
Basic Indexing
Matrix indexing in R is done using the [row, column] notation. Here are some basic examples:
Accessing a Single Element
To access a single element, specify its row and column indices:
# Example 1: Accessing a single element m <- matrix(1:9, nrow = 3) # Access element in 2nd row, 3rd column element <- m[2, 3] print(element) # The result is: # 6
Accessing a Row
To access an entire row, use row_index, :` for all columns:
# Example 2: Accessing a row row_2 <- m[2, ] print(row_2) # The result is: # [1] 4 5 6
Accessing a Column
To access an entire column, use , column_index:
# Example 3: Accessing a column col_3 <- m[, 3] print(col_3) # The result is: # [1] 3 6 9
Slicing a Matrix
Slicing allows you to extract a subset of the matrix. You can specify ranges for rows and columns:
Extracting a Submatrix
To extract a submatrix, specify ranges for rows and columns:
# Example 4: Extracting a submatrix submatrix <- m[1:2, 2:3] print(submatrix) # The result is: # [,1] [,2] # [1,] 2 3 # [2,] 5 6
Using Logical Indexing
Logical indexing lets you select elements based on conditions:
# Example 5: Logical indexing logical_index <- m > 5 print(logical_index) # Extract elements greater than 5 elements_gt_5 <- m[logical_index] print(elements_gt_5) # The results are: # Logical matrix # [,1] [,2] [,3] # [1,] FALSE FALSE FALSE # [2,] FALSE FALSE TRUE # [3,] FALSE FALSE TRUE # Extracted elements # [1] 6 9
Indexing with Vectors
You can use vectors for indexing rows or columns, which allows for more flexible selections:
Using a Vector for Rows
# Example 6: Indexing with a vector for rows rows_to_extract <- c(1, 3) subset_rows <- m[rows_to_extract, ] print(subset_rows) # The result is: # [,1] [,2] [,3] # [1,] 1 2 3 # [2,] 7 8 9
Using a Vector for Columns
# Example 7: Indexing with a vector for columns cols_to_extract <- c(2, 3) subset_cols <- m[, cols_to_extract] print(subset_cols) # The result is: # [,1] [,2] # [1,] 2 3 # [2,] 5 6 # [3,] 8 9
Modifying Matrix Elements
You can modify matrix elements by assigning new values using indexing:
Modifying a Single Element
# Example 8: Modifying a single element m[1, 2] <- 99 print(m) The result is: # [,1] [,2] [,3] # [1,] 1 99 3 # [2,] 4 5 6 # [3,] 7 8 9
Modifying an Entire Row or Column
# Example 9: Modifying an entire row m[2, ] <- c(10, 11, 12) print(m)
# Example 10: Modifying an entire column m[, 3] <- c(13, 14, 15) print(m) # The results are: # Modified row # [,1] [,2] [,3] # [1,] 1 99 3 # [2,] 10 11 12 # [3,] 7 8 15 # Modified column # [,1] [,2] [,3] # [1,] 1 99 13 # [2,] 10 11 14 # [3,] 7 8 15
Named Indexing
You can also index matrices using row and column names, which can make the code more readable:
Adding Row and Column Names
# Example 11: Adding row and column names m <- matrix(1:9, nrow = 3) rownames(m) <- c("Row1", "Row2", "Row3") colnames(m) <- c("Col1", "Col2", "Col3") print(m) # The result is: # Col1 Col2 Col3 # Row1 1 4 7 # Row2 2 5 8 # Row3 3 6 9
Indexing with Names
# Example 12: Indexing with names named_element <- m["Row2", "Col3"] print(named_element) # The result is: # 8
Summary of Matrix Indexing
- Accessing a Single Element: [row, column]
- Accessing a Row: [row, ]
- Accessing a Column: [ , column]
- Extracting a Submatrix: [rows_range, cols_range]
- Logical Indexing: Using logical conditions
- Indexing with Vectors: Using vectors for rows or columns
- Modifying Elements: Assigning new values
- Named Indexing: Using row and column names
Matrix indexing in R provides powerful tools to access, manipulate, and modify matrices effectively. Understanding these techniques allows for efficient data manipulation and analysis.