1
我想使用data.table
作爲aggregate()
或ddply()
的替代方法,因爲這兩種方法並不像所希望的那樣有效地擴展爲大對象。不幸的是,我還沒有想出如何獲得向量返回的聚合函數以在data.table
的結果中生成多個列。例如:從data.table彙總中返回多列
# required packages
library(plyr)
library(data.table)
# simulated data
x <- data.table(value=rnorm(100), g=rep(letters[1:5], each=20))
# ddply output that I would like to get from data.table
ddply(data.frame(x), 'g', function(i) quantile(i$value))
g 0% 25% 50% 75% 100%
1 a -1.547495 -0.7842795 0.202456288 0.6098762 2.223530
2 b -1.366937 -0.4418388 -0.085876995 0.7826863 2.236469
3 c -2.064510 -0.6411390 -0.257526983 0.3213343 1.039053
4 d -1.773933 -0.5493362 -0.007549273 0.4835467 2.116601
5 e -0.780976 -0.2315245 0.194869630 0.6698881 2.207800
# not quite what I am looking for:
x[, quantile(value), by=g]
g V1
1: a -1.547495345
2: a -0.784279536
3: a 0.202456288
4: a 0.609876241
5: a 2.223529739
6: b -1.366937074
7: b -0.441838791
8: b -0.085876995
9: b 0.782686277
10: b 2.236468703
本質上,從ddply
aggregate
和所述輸出是在寬的格式,而從data.table
輸出爲長格式。答案是否重塑了我的data.table
對象的數據或其他參數?
似乎是同樣的問題在這裏http://stackoverflow.com/questions/16150153/create-columns-from-column-of-list-in-回答數據表?RQ = 1 – Dylan