我想知道是否可以按一定的行数(例如每5行)对data.table上的数据进行操作?我可以通过添加一个额外的索引列并获得如下结果来做到这一点。我想知道是否可以在不添加索引列的情况下做同样的事情,因为我的数据有数百万行,添加一个新列,只会消耗内存。感谢您的建议。
library(data.table)
DT <- data.table(A = 1:20, B = sample(1000, 20), C = sample(10, 20, replace = T))
# Can I work on the DT, and get the result as following,
# without adding extra column D, like DT.1?
DT.1 <- cbind(DT, D=rep(1:5, each=4))
DT.1[, .(MeanA=mean(A), MeanB=mean(B), MeanC=mean(C)), by="D"][,.(MeanA, MeanB, MeanC)]
# MeanA MeanB MeanC
#1: 2.5 688.50 4.50
#2: 6.5 656.00 4.25
#3: 10.5 610.50 8.75
#4: 14.5 451.75 5.50
#5: 18.5 423.00 4.75