假设我有:

set.seed(42)
d = data.frame(replicate(6,rnorm(10)))
col_labels = c("a", "a", "b", "b", "c", "c")
row_labels = c(1,1,1,2,2,3,3,4,4,4)

我现在要计算与dcol_labels的每种组合相对应的row_labels子集的平均值,即:
s = subset(d, row_labels==1, select=col_labels=="a")
s_mean = mean(as.matrix(s))

最后,我想要一个数据框,其行对应于row_labels,列对应于col_labels,并设置子集的平均值。没有大量for循环该如何执行?

最佳答案

这是另一个选择:

res <- lapply(split.default(d, col_labels), FUN=by, INDICES=list(row_labels), function(x) mean(unlist(x)))
do.call(rbind, res)
#          1       2       3       4
# a  0.56201  0.1563  0.4393 -0.3193
# b -0.01075  0.7515 -0.7973 -0.8620
# c  0.28615 -0.3406  0.1443 -0.1583

关于r - R:行和列标签上的数据框的子集的平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26018355/

10-12 19:53