你可以这样做:
do.call(rbind,by(dfex[,-(1:2)], paste("mean-id",dfex[,1],sep="-"), colMeans))
var3 var4 var5 var6
mean-id-1 -0.7383944 0.5005763 -0.4777325 0.6988741
mean-id-2 -0.0316267 -0.1764453 0.1313834 0.6867287
mean-id-3 0.7489377 0.8091953 0.9290247 -0.1263163
将两个结果创建为列表:
lapply(c("id","status"), function(x) do.call(rbind,by(dfex[grep("var",names(dfex))], paste("mean-id",dfex[,x],sep="-"), colMeans)))
更新:
library(matrixStats)
lapply(c("id","status"), function(x) do.call(rbind,by(dfex[grep("var",names(dfex))], paste("mean-id",dfex[,x],sep="-"), colSds)))
[[1]]
var3 var4 var5 var6
mean-id-1 0.6024318 1.36423044 0.5398717 0.7260939
mean-id-2 0.2623706 0.08870122 0.1827246 1.0590560
mean-id-3 1.0625137 0.16381062 1.0760977 0.3524908
[[2]]
var3 var4 var5 var6
mean-id-hit 0.4369311 1.036234 0.6622341 0.6506010
mean-id-miss 0.8288436 1.035163 0.7688912 0.6799636