如何基于列索引对矩阵分组子矩阵执行行求和?
Got it, let's break down how to solve this problem. You've got a matrix where certain columns are related, and you want to collapse those related columns into single columns by summing the rows within each group. Here's a straightforward way to do this using base R—no extra packages needed.
Step 1: Recreate Your Original Matrix
First, let's make sure we're working with the same data you provided. We can build your 4x16 matrix like this:
# Build the original 4x16 matrix original_matrix <- matrix(1:64, nrow = 4, byrow = TRUE) print(original_matrix)
This gives you exactly the matrix you shared:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [1,] 1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 [2,] 2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 [3,] 3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 [4,] 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64
Step 2: Define Your Column Groups
The key here is to specify which columns belong to each group. Let's say your 16 columns are split into 5 groups (matching your example's 5-column output). For example, let's define groups like this (adjust this to match your actual column relationships):
# Define which column belongs to which group (length matches number of columns) col_groups <- c(1,1,1,1, # Group 1: columns 1-4 2,2,2,2, # Group 2: columns 5-8 3,3, # Group 3: columns 9-10 4,4, # Group 4: columns 11-12 5,5,5,5) # Group 5: columns 13-16
Step 3: Calculate Row Sums for Each Group
Now we'll use base R functions to split the columns by group, sum the rows in each group, and reshape into the final matrix:
# Transpose the matrix to work with columns as rows, split by group, sum, then transpose back new_matrix <- t(sapply(split(t(original_matrix), col_groups), colSums)) print(new_matrix)
Let's break down what this does:
t(original_matrix): Flips the matrix so original columns become rows—this makes it easy to split by our column groups.split(t(original_matrix), col_groups): Splits the transposed matrix into submatrices based on our group definition.sapply(..., colSums): Calculates the column sums for each submatrix (which are the row sums of the original grouped columns).t(): Transposes the result back to get the original row structure, with each column representing a group sum.
For Equal-Sized Column Groups
If your related columns are all the same size (e.g., every 4 columns form a group), you can simplify the code even more:
# Set the size of each column group group_size <- 4 num_groups <- ncol(original_matrix) / group_size # Calculate row sums for each equal-sized group new_matrix_equal <- sapply(1:num_groups, function(group_num) { # Get the columns in the current group group_cols <- ((group_num - 1) * group_size + 1):(group_num * group_size) # Sum rows for those columns rowSums(original_matrix[, group_cols]) }) print(new_matrix_equal)
Customizing to Match Your Exact Output
If your target output's first value is 15 (like you mentioned), that suggests you might want to sum rows across related rows instead of columns. The logic stays nearly identical—just swap row and column operations. For example, to sum rows 1-4 into a single row, you'd use colSums(original_matrix) and reshape as needed.
内容的提问来源于stack exchange,提问作者Björn

