Calculating a weighted mean using data.table in R with weights in one of the table columns -
i have data.table shown below. i'm trying calculate weighted mean subsets of data. i've tried 2 approaches mwe below
set.seed(12345) dt = data.table(a =c(10,20,25,10,10),b=rnorm(5),c=rnorm(5),d=rnorm(5),e=rnorm(5)) dt$key = sample(toupper(letters[1:3]),5,replace=t) setkey(dt, key) first subsetting .sd , using lapply call, doesnt work (and wasn't expected to)
dt[,lapply(.sd,function(x) weighted.mean(x,.sd[1])),by=key] second trying define function apply .sd if using ddply.
this fails too.
wmn=function(x){ tmp = null for(i in 2:ncol(x)){ tmp1 = weighted.mean(x[,i],x[,1]) tmp = c(tmp,tmp1) } return(tmp) } dt[,wmn,by=key] any thoughts on how best this?
thanks
edit
change error on wmn formula on columns selected.
second edit
weighted mean formula reversed , added set.seed
if want take weighted means of "b"..."e" using "a" weight, think trick:
dt[,lapply(.sd,weighted.mean,w=a),by=key,.sdcols=letters[1:5]]
Comments
Post a Comment