Filtering Matrices in R
Filtering with Logical Conditions
You can filter matrices using logical conditions. Logical conditions return a logical matrix where each element is TRUE or FALSE based on the specified condition.
Example of Filtering with a Logical Condition
Suppose you have a matrix and want to extract elements greater than a certain value:
# Example 1: Filtering with a logical condition m <- matrix(1:9, nrow = 3) # Create a logical matrix where elements are greater than 5 logical_matrix <- m > 5 print(logical_matrix) # Extract elements greater than 5 elements_gt_5 <- m[logical_matrix] print(elements_gt_5) # The result is: # Logical matrix # [,1] [,2] [,3] # [1,] FALSE FALSE FALSE # [2,] FALSE FALSE TRUE # [3,] FALSE FALSE TRUE # Extracted elements # [1] 6 7 8 9
Filtering Based on Submatrices
You can also filter by extracting submatrices that meet certain conditions.
Example of Filtering to Extract Submatrices
Suppose you want to extract all rows where the value in the second column is greater than 5:
# Example 2: Filtering to extract submatrices m <- matrix(1:9, nrow = 3) colnames(m) <- c("Col1", "Col2", "Col3") # Extract rows where the value in column "Col2" is greater than 5 submatrix <- m[m[, "Col2"] > 5, ] print(submatrix) # The result is: # Col1 Col2 Col3 # [1,] 7 8 9
Filtering with Multiple Criteria
You can combine multiple conditions to perform more complex filtering. Use logical operators (&, |, !) to combine conditions.
Example of Filtering with Multiple Criteria
Filter rows where the value in the first column is less than 8 AND the value in the third column is greater than 5:
# Example 3: Filtering with multiple criteria m <- matrix(1:9, nrow = 3) colnames(m) <- c("Col1", "Col2", "Col3") # Create a logical matrix with multiple criteria logical_matrix_multiple <- m[, "Col1"] < 8 & m[, "Col3"] > 5 print(logical_matrix_multiple) # Extract rows that meet the criteria submatrix_multiple <- m[logical_matrix_multiple, ] print(submatrix_multiple) # The result is: # Logical matrix with multiple criteria # [1] FALSE TRUE FALSE # Extracted submatrix # Col1 Col2 Col3 #[1,] 7 8 9
Using apply() for Filtering
The apply() function can be used to apply functions over margins of a matrix and to filter rows or columns based on the results.
Example of Filtering with apply()
Filter rows where the sum of elements is greater than 15:
# Example 4: Filtering with apply() m <- matrix(1:9, nrow = 3) # Calculate the sum of elements for each row row_sums <- apply(m, 1, sum) print(row_sums) # Extract rows where the sum of elements is greater than 15 filtered_rows <- m[row_sums > 15, ] print(filtered_rows) # The result is: # Sum of elements for each row # [1] 6 15 24 # Extracted rows # [,1] [,2] [,3] # [3,] 7 8 9
Filtering with subset()
The subset() function can also be used for filtering, especially when matrices are converted to data frames.
Example of Filtering with subset()
Convert the matrix to a data frame and filter rows based on conditions:
# Example 5: Filtering with subset() m <- matrix(1:9, nrow = 3) df <- as.data.frame(m) colnames(df) <- c("Col1", "Col2", "Col3") # Filter rows where the value in "Col2" is greater than 5 filtered_df <- subset(df, Col2 > 5) print(filtered_df) The result is: # Col1 Col2 Col3 # 3 7 8 9
Summary of Matrix Filtering
- Filtering with Logical Conditions: Use logical conditions to create logical matrices and extract elements.
- Filtering Based on Submatrices: Extract submatrices based on conditions applied to rows or columns.
- Filtering with Multiple Criteria: Combine multiple conditions using logical operators for more complex filtering.
- Using apply(): Apply functions across matrix margins to filter rows or columns.