title: R Error: number of items to replace is not a multiple of replacement length
body: <p>Good afternoon.  I am working in R (in RStudio specifically) and have an error which I have seen in other threads but have been unable to translate the solutions to my own code.</p>
  
  <p>I have quite a lengthy "if" loop (with nested "else" and "for" statements), however the key parts of my code are that the variable BCmth is defined as:</p>
  
  <pre><code>BCmth&lt;- matrix(NA, nrow = 3000000,ncol = 4)
  </code></pre>
  
  <p>and BCstore as:</p>
  
  <pre><code>BCstore&lt;- matrix(1,1000,2*round(iForeper))
  </code></pre>
  
  <p>And in this section of the loop they are used as such:</p>
  
  <pre><code>BCmth[,4] &lt;- cumsum(BCmth[,3]) # compute cumulative density booking curve
  BCstore[1:size(BCmth[,4],1),m] &lt;- BCmth[,2] # store the month for the booking curve
  BCstore[1:size(BCmth[,4],1),m+1] &lt;- BCmth[,4] / BCmth[length(BCmth):4]  # store the booking curve  value as a %-age 
  BCmref[1,m] &lt;- BCmth[1,1] 
  m &lt;- m+2
  } 
  </code></pre>
  
  <p>And I am receiving the below error after a run of the entire loop:</p>
  
  <pre><code>Error in BCstore[1:size(BCmth[, 4], 1), m] &lt;- BCmth[, 2] : 
    number of items to replace is not a multiple of replacement length
  </code></pre>
  
  <p>PLEASE NOTE: I am using the matlab package and the "size" function used here is the function relating to Matlab's usage of the word.</p>
  
  <p>Thank you in advance for any help.</p>