在矩阵中按组求和

时间:2015-04-05 21:53:32

标签: r matrix

假设我有一个名为x的矩阵。

x <- structure(c(1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1), 
.Dim = c(5L, 4L), .Dimnames = list(c("Cake", "Pie", "Cake", "Pie", "Pie"),
c("Mon", "Tue", "Wed", "Thurs"))) 

x
     Mon   Tue   Wed   Thurs
Cake   1     0     1      1
Pie    0     0     1      1
Cake   1     1     0      1
Pie    0     0     1      1
Pie    0     0     1      1

我希望它成为:

     Mon   Tue   Wed   Thurs
Cake   2     1     1      2
Pie    0     0     3      3

我尝试过使用addmargins(x),但这只是给了我每列和每行的总和。有什么建议?我搜索了其他问题,但无法解决这个问题。

3 个答案:

答案 0 :(得分:7)

这是一个矢量化基础解决方案

rowsum(df, row.names(x))
#      Mon Tue Wed Thurs
# Cake   2   1   1     2
# Pie    0   0   3     3

或使用data.table的{​​{1}}版本,以便将您的行名称转换为列

keep.rownames = TRUE

答案 1 :(得分:6)

你可以试试这个

df <- read.table(head=TRUE, text="
Name       Mon   Tue   Wed   Thurs
Cake   1     0     1      1
Pie    0     0     1      1
Cake   1     1     0      1
Pie    0     0     1      1
Pie    0     0     1      1")

aggregate(. ~ Name, data=df, FUN=sum)
##   Name Mon Tue Wed Thurs
## 1 Cake   2   1   1     2
## 2  Pie   0   0   3     3

还有dplyr

library(dplyr)
group_by(df, Name) %>%
    summarise(Mon = sum(Mon), Tue = sum(Tue), Wed = sum(Wed), Thurs = sum(Thurs))

或更好

 group_by(df, Name) %>%
    summarise_each(funs(sum))

答案 2 :(得分:2)

使用plyr的方法:

ldply(split(df, df$Name), function(u) colSums(u[-1]))
#   .id Mon Tue Wed Thurs
#1 Cake   2   1   1     2
#2  Pie   0   0   3     3

数据:

df = structure(list(Name = structure(c(1L, 2L, 1L, 2L, 2L), .Label = c("Cake", 
"Pie"), class = "factor"), Mon = c(1L, 0L, 1L, 0L, 0L), Tue = c(0L, 
0L, 1L, 0L, 0L), Wed = c(1L, 1L, 0L, 1L, 1L), Thurs = c(1L, 1L, 
1L, 1L, 1L)), .Names = c("Name", "Mon", "Tue", "Wed", "Thurs"
), row.names = c(NA, -5L), class = "data.frame")