title: Weighted mean by row
body: <p>I have the following data:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I want to get the weighted mean for each record.  So I tried this:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>Which returned the following error message:</p>
  
  <pre><code>Error in weighted.mean.default(x, weight) : 
    'x' and 'w' must have the same length
  </code></pre>
  
  <p>So then I tried this:</p>
  
  <pre><code>weight=as.vector(weight)
  z$weightMean=apply(subset(z,select=c(a,b,c)),1,function(x) weighted.mean(x,weight))
  </code></pre>
  
  <p>Which also returned the same error.</p>
  
  <p>What am I doing wrong?</p>