title: Combining nested for-loop outputs into a vector
body: <p>I fitted a Multistage Markov (MSM) model using 3 covariates. I’m trying to apply a function whose main arguments are fitted MSM model and values of covariates. The function is:</p>
  
  <pre><code>transition&lt;-qmatrix.msm(MSMmod,  ci="normal", covariates=list(grossTon= 10, activ=0, period=1))
  </code></pre>
  
  <p>An output of this function are the estimate values:</p>
  
  <pre><code>              Owning&lt;10 Owning10-40   Owning&gt;40        left
  Owning&lt;10   -0.18037446  0.06140559  0.00000000 0.118968868
  Owning10-40  0.01609661 -0.09027454  0.04502546 0.029152476
  Owning&gt;40    0.00000000  0.04801757 -0.05137377 0.003356196
  left         0.00000000  0.00000000  0.00000000 0.000000000
  </code></pre>
  
  <p>and I’m interested just in some of them, so with <code>transition$estimates[c(5,2,10,7,13,14,15)]</code> I’m getting; for example,  the values that the function <code>qmatrix.msm</code> gives with the covariates combination grossTon=120, active=0 and period=1 (see below).</p>
  
  <pre><code>[1] 0.06140559 0.01609661 0.04502546 0.04801757 0.118968868 0.029152476 0.003356196
  </code></pre>
  
  <p>With both for loops in my function I'm expecting get 48 vector (7 values every one,similar to the previous one) as a result of all combinations of grossTon (12 values) and activ(4 values),  thus finally to combine all of them into a single vector (336 values, 7*12*4).</p>
  
  <p>This is my function:</p>
  
  <pre><code>transRate&lt;-function(period){
    estim&lt;-data.frame(matrix(rep(0,336),336,1))
  
    for(i in seq(10,120,by=10)){
      for(j in seq(0,3, by=1)){
  
      estim[c(i,j)]&lt;-qmatrix.msm(msm.Mult4,  ci="normal", covariates=list(grossTon=i, activ=j, period=period))$estimates[c(5,2,10,7,13,14,15)]
      outp[c(i,j)]&lt;-c(estim[c(i,j)])#Here I'm trying to get my 336 values vector
      }
    }
  
    grosTvect&lt;-sort(rep(seq(10,120,by=10),28))
    rate&lt;-rep(c("q12","q21","q23","q32","q14","q24","q34"),48)
    estimRate&lt;-data.frame(grosTvect,rate,outp)
  
    return(estimRate)                     
  } 
  dataFrame&lt;-transRate(period=1)
  </code></pre>
  
  <p>I don’t know how to deal with i and j to produce my vector.  </p>
  
  <p>I'm getting the following error when I write <code>estim[c(i,j)]</code> 
  and <code>outp[c(i,j)]&lt;-c(estim[c(i,j)])</code></p>
  
  <pre><code>error in `[&lt;-.data.frame`(`*tmp*`, c(i, j), value = c(0.0614055886960195,  : 
    new columns would leave holes after existing columns
  </code></pre>
  
  <p>And this other when I write <code>estim[i,j]</code> and <code>outp&lt;-estim[i,j</code>]</p>
  
  <pre><code>Error in `[&lt;-.data.frame`(`*tmp*`, i, j, value = c(0.0614055886960195,  : 
    replacement has 7 rows, data has 1
  </code></pre>
  
  <p>Any help will be appreciated.</p>