Let's say I have a matrix called 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
I want to sum each column grouped by row names:
Mon Tue Wed Thurs
Cake 2 1 1 2
Pie 0 0 3 3
I've tried using addmargins(x)
, but that just gives me the sum of each column and row. Any suggestions? I searched other questions, but couldn't figure this out.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…