title: Why do I get “number of items to replace is not a multiple of replacement length”
body: <p>I am creating a matrix, eat_matrix, with info from other files. I have created my rows as dive numbers and my column as the prey capture number. Yet with the following script I get the error: </p>
  
  <blockquote>
    <p>Error in eat_matrix[dive_no[cols == cls], cls] &lt;- stat[,cols == cls] : 
      number of items to replace is not a multiple of replacement length</p>
  </blockquote>
  
  <p>Script: </p>
  
  <pre class="lang-r prettyprint-override"><code>for(cls in 1:ncol(eat_matrix)){
    eat_matrix[dive_no[cols == cls],cls] &lt;- stat[cols == cls]
  }
  </code></pre>
  
  <p>I can see it means a mismatch in length, but I don't understand how to fix this. Can anyone help? Thank you!</p>
  
  <p>Edited: this is the code for stat</p>
  
  <pre><code>tst &lt;- is.na(dive_no)tst &lt;- is.na(dive_no)
  dive_no &lt;- dive_no[!tst]
  cols &lt;- cols[!tst]
  prey_notInDive &lt;- prey[tst,]
  
  eat_matrix &lt;- matrix(nrow=nrow(dives),ncol=max(len))
  stat &lt;- prey$max_Stat[!tst]
  </code></pre>