我遇到了问题,但我确信这对于非常熟悉R的人来说非常容易。 我有一个3008 x 3008的矩阵。我想要的是每行中每8列的总和。所以基本上你最终会得到一个新的矩阵,现在是367 x 367.
这是一个小例子:
C.1 C.2 C.3 C.4 C.5 C.6
row1 1 2 1 2 5 6
row1 1 2 3 4 5 6
row1 2 6 3 4 5 6
row1 1 2 3 4 10 6
所以说我想在每一行中每3列加上这些,我想最终得到:
C.1 C.2
row1 4 13
row1 6 15
row1 11 15
row1 6 20
答案 0 :(得分:2)
# m is your matrix
n <- 8
grp <- seq(1, ncol(m), by=n)
sapply(grp, function(x) rowSums(m[, x:(x+n-1)]))
如果您是R. grp
的新手,则会有一些解释是一系列数字,它们为每组列提供起点:1,9,17等,如果您想要每8列求和一次。
sapply
电话可以理解如下。对于grp
中的每个数字,它调用rowSums
函数,并将与该组编号对应的矩阵列传递给它。因此,当grp
为1时,它获得第1-8列的行总和;当grp
为9时,它获取第9-16列的行总和,依此类推。这些是向量,sapply
然后将它们组合成一个矩阵。
答案 1 :(得分:1)
将矩阵转换为数组,然后使用apply
和rowSums
。
mat <- structure(c(1L, 1L, 2L, 1L, 2L, 2L, 6L, 2L, 1L, 3L, 3L, 3L, 2L, 4L, 4L, 4L, 5L, 5L, 5L, 10L, 6L, 6L, 6L, 6L),
.Dim = c(4L, 6L),
.Dimnames = list(c("row1", "row2", "row3", "row4"), c("C.1", "C.2", "C.3", "C.4", "C.5", "C.6")))
n <- 3 #this needs to be a factor of the number of columns
a <- array(mat,dim=c(nrow(mat),n,ncol(mat)/n))
apply(a,3,rowSums)
# [,1] [,2]
# [1,] 4 13
# [2,] 6 15
# [3,] 11 15
# [4,] 6 20
答案 2 :(得分:0)
#Create sample data:
df <- matrix(rexp(200, rate=.1), ncol=20)
#Choose the number of columns you'd like to sum up (e.g., 3 or 8)
number_of_columns_to_sum <- 3
df2 <- NULL #Set to null so that you can use cbind on the first value below
for (i in seq(1,ncol(df), by = number_of_columns_to_sum)) {
df2 <- cbind(df2, rowSums(df[,i:(i+number_of_columns_to_sum-1)]))
}
答案 3 :(得分:0)
另一种选择:虽然它可能不那么优雅
mat <- structure(c(1L, 1L, 2L, 1L, 2L, 2L, 6L, 2L, 1L, 3L, 3L, 3L, 2L, 4L, 4L, 4L, 5L, 5L, 5L, 10L, 6L, 6L, 6L, 6L),
.Dim = c(4L, 6L),
.Dimnames = list(c("row1", "row1", "row1", "row1"), c("C.1", "C.2", "C.3", "C.4", "C.5", "C.6")))
new<- data.frame((mat[,1]+mat[,2]+mat[,3]),(mat[,4]+mat[,5]+mat[,6]))
names(new)<- c("C.1","C.2")
new