简体   繁体   中英

R data.table syntax for subsetting and summarising

This is probablly quite simple but would like to be able to summarise some data (mean and median) based upon on random column selection, and for it to be grouped by a different column.

Please see below:

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

If for example ww happened to equal "v" then I would expect the following output

   x avg med
1: a   2   2  
2: b   5   5
3: c   8   8

I think it is just syntax that I need to adjust, but am unsure how to adjust it...Any help would be greatly appreciated...

You need to use 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM