title: R error: number of items to replace is not a multiple of replacement length R
body: <p>I wrote the following code and got the error: number of items to replace is not a multiple of replacement length at code line: </p>
  
  <pre><code> X_after[count, ] = c(censN1, censN2, censN3)
  </code></pre>
  
  <p>After searching the internet, I found the problem is probably caused by the unmatched number of samples size of the pre-determine <code>n_samples</code> of <code>NA</code> and the final  <code>X_after</code> dataset.  How can I adjust the matrix code such that <code>ncol</code> is dynamically determined after the loop rather than pre-determined at n_samples?  Or if you have other solutions to this error message, please chime in as well.</p>
  
  <pre><code>multiLodSim &lt;- function (GM, GSD, n_samples, n_iterations, p) {    
    X_after &lt;- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
    delta &lt;- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
    mu &lt;- log(GM)
    sigma &lt;- log(GSD)
    lod1 &lt;- quantile(rlnorm(100000,mu,sigma),p)
    lod2 &lt;-  quantile(rlnorm(100000,mu,sigma),(p*0.95))
    lod3 &lt;- quantile(rlnorm(100000,mu,sigma),(p*0.9)) 
    pct_cens &lt;- numeric(n_iterations)
    count &lt;- 1
     while(count &lt;= n_iterations) {     
     sub_samples = n_samples/3   # divide the total sample into third (for 3 lods)
    n1 &lt;- rlnorm(sub_samples,mu,sigma)
  censN1 &lt;- sort(pmax(n1,lod1))   
  n2 &lt;- rlnorm(sub_samples,mu,sigma)
  censN2 &lt;- sort(pmax(n2,lod1))
  censN2[censN2==lod1] &lt;- lod2   
  n3 &lt;- rlnorm(sub_samples,mu,sigma)
  censN3 &lt;- sort(pmax(n3,lod1))    
  censN3 [censN3==lod1] &lt;- lod3
  X_after[count, ] = c(censN1, censN2, censN3)
  delta [count, ] = X_after &lt;= lod1  # nondetects= TRUE (1), detects= FALSE (0)
  pct_cens [count] = mean(delta[count,])   #
  if (pct_cens [count]  &gt; 0 &amp; pct_cens [count] &lt; 1 ) count &lt;- count + 1}}
  
   a = multiLodSim(GM=1,GSD=2,n_samples=20,n_iterations=5,p=0.3)
  </code></pre>
  
  <p>Updates:  After reading your comments, I made changes to these code lines and it is working. Thank you for your help.  </p>
  
  <pre><code>n1 = rlnorm(round(sub_samples),mu,sigma)
  n2 = rlnorm(round(sub_samples),mu,sigma)
  sub_samples3 = n_samples - length(n1)-length(n2)
  n3 = rlnorm(subsamples3, mu,sigma)
  </code></pre>