title: R: using apply over two data.frames
body: <p>I want to use apply instead of a for-loop. The problem is, my for-loop uses two data.frames as an input. For example:</p>
  
  <pre><code>x &lt;- data.frame(col1=c(1,NA,3,NA), col2=c(9,NA,11,12))
  y &lt;- data.frame(col1=c(1,2,3,4), col2=c(5,6,7,8))
  output &lt;- rep(NA,2)
  for(i in 1:2)
  {
    output[i] &lt;- sum(is.na(x[,i]))+sum(y[,i])
  }
  </code></pre>
  
  <p>The result here is, correctly c(12,27).</p>
  
  <p>But if I try function and apply:</p>
  
  <pre><code>test &lt;- function(vector1,vector2) sum(is.na(vector1))+sum(vector2)
  apply(x,y,MARGIN=2,FUN=test)
  </code></pre>
  
  <p>With apply the result is c(38,37). 
  How can I fix this?</p>