Skip to content
Prev 206443 / 398503 Next

How to calculate the row wise means for grouped columns in matrix?

adapted from the help files of rowsum

x <- matrix(runif(100), ncol=5)
group <- sample(1:8, 20, TRUE)
xsum <- rowsum(x, group)
sweep(xsum, 1, table(group), "/")

or

aggregate(x, list(group), mean)[-1]

b

2010/1/15 Joel F?rstenberg-H?gg <joel_furstenberg_hagg at hotmail.com>: