12345678_0001
第三个很好的选择是使用这个包。data.table,它也具有类data.framework,但是像您正在寻找的操作计算速度要快得多。library(data.table)mydt <- structure(list(Name = c("Aira", "Aira", "Aira", "Ben", "Ben", "Ben", "Cat", "Cat", "Cat"), Month =
c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Rate1 = c(15.6396600443877, 2.15649279424609, 6.24692918928743, 2.37658797276116, 34.7500663272292,
3.28750138697048, 29.3265553981065, 17.9821839334431, 10.8639802575958), Rate2 = c(17.1680489538369, 5.84231656330206, 8.54330866437461, 5
.88415184986176, 3.02064294862551, 17.2053351400752, 16.9552950199166, 2.56058000170089, 15.7496228048122)), .Names = c("Name", "Month",
"Rate1", "Rate2"), row.names = c(NA, -9L), class = c("data.table", "data.frame"))现在取所有3个月的比率1和比率2的平均值,对于每个人(姓名):首先,决定你想取哪个列的平均值。colstoavg <- names(mydt)[3:4]现在我们使用lapplication对我们想要的列取平均值(Colstoavg)。mydt.mean <- mydt[,lapply(.SD,mean,na.rm=TRUE),by=Name,.SDcols=colstoavg]
mydt.mean
Name Rate1 Rate21: Aira 8.014361 10.5178912: Ben 13.471385 8.7033773: Cat 19.390907 11.755166