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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With