这可能非常简单,但希望能够根据随机列选择汇总一些数据(平均值和中值),并将其按不同的列进行分组。
请参阅以下内容:
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
ww <- sample(c("y","v"),1)
DT[,list(avg=mean(ww),med=median(ww)),by="x"]
x avg med
1: a NA y
2: b NA y
3: c NA y
Warning messages:
1: In `[.data.table`(DT, , list(avg = mean(ww), med = median(ww)), :
argument is not numeric or logical: returning NA
2: In `[.data.table`(DT, , list(avg = mean(ww), med = median(ww)), :
argument is not numeric or logical: returning NA
3: In `[.data.table`(DT, , list(avg = mean(ww), med = median(ww)), :
argument is not numeric or logical: returning NA
例如,如果ww恰好等于"v"
,那么我希望得到以下输出
x avg med
1: a 2 2
2: b 5 5
3: c 8 8
我认为这只是我需要调整的语法,但我不确定如何调整它......任何帮助都会非常感激......
答案 0 :(得分:6)
您需要使用get
:
> DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
> ww <- sample(c("y","v"),1)
> DT[,list(avg=mean(get(ww)),med=median(get(ww))),by="x"]
x avg med
1: a 3.333333 3
2: b 3.333333 3
3: c 3.333333 3
> ww
[1] "y"