I have the following data:
a=c(1:10)
b=c(16:25)
c=c(24:33)
wa=c(3,7,3,3,3,3,3,3,3,1)
wb=c(3,2,3,3,3,3,3,3,3,8)
wc=c(4,1,4,4,4,4,4,4,4,1)
z=data.frame(a,b,c,wa,wb,wc)
I want to get the weighted mean for each record. So I tried this:
weight=apply(subset(z,select=c(wa,wb,wc)),1,function(x) x)
z$weightMean=apply(subset(z,select=c(a,b,c)),1,function(x) weighted.mean(x,weight))
Which returned the following error message:
Error in weighted.mean.default(x, weight) :
'x' and 'w' must have the same length
So then I tried this:
weight=as.vector(weight)
z$weightMean=apply(subset(z,select=c(a,b,c)),1,function(x) weighted.mean(x,weight))
Which also returned the same error.
What am I doing wrong?
See Question&Answers more detail:
os