I have a data.table with several variables (columns) and their standard errors. I'd like to calculate several things such as the weighted mean, the mean of the standard error, p-values...
Toy example:
myDT <- data.table(ID=1:8, AA=21:28, BB=31:38, CC=41:48, stdAA=(11:18)/10, stdBB=(11:18)/10, stdCC=(11:18)/10)
mycol <- c("AA", "BB", "CC") # Select these ones.
OUTPUT1 <- myDT[,.(Variables=mycol,Estimate=lapply(mycol, function (x) weighted.mean(get(x),1/(get(paste0("std",x)))^2 ) ),
Std.Error=lapply(mycol, function (x) mean(get(paste0("std",x)))))]
OUTPUT1[,tvalue := as.numeric(Estimate)/as.numeric(Std.Error) ][,pvalue := 2*pt(-abs(tvalue),df=7)]
As you see I've used "as.numeric" because otherwise I'm getting the error "non-numeric argument to binary operator",
I guess the problem is I the data.table contains lists instead of vectors.
I would like to get the same but simpler, inserting everything inside the same lapply.
How can I do it?
The following code doesn't produce the expected output because it inserts a vector on each element of the second column, they I can't further work with it.
OUTPUT2 <- myDT[,.(mycol,lapply(mycol, function (x)
c(weighted.mean(get(x),1/(get(paste0("std",x)))^2 ),
mean(get(paste0("std",x))) )))]
Maybe I need to add some kind of rbindlist but I haven't be able to do it properly.
My next try works though it's not shorter. (And I haven't event added columnames because they get removed, I need to add them later)
OUTPUT3 <- data.table(mycol,t(myDT[,c(lapply(mycol, function (x) .(weighted.mean(get(x),1/(get(paste0("std",x)))^2 ), mean(get(paste0("std",x))))))]))
OUTPUT3[,tvalue := as.numeric(V1)/as.numeric(V2) ][,pvalue := 2*pt(-abs(tvalue),df=7)]
It would be great to have all calculations inside a single lapply.