title: Fast and efficient way to expand a dataset in R
body: <p>I try to expand a dataset in R using the values from different columns (Key2 - KeyX) and then use the column number in a formula do compute some value. </p>
  
  <p>Example of a part of the dataset I want to expand</p>
  
  <pre><code>Year Key2 Key3 Key4 Key5 ...
  2001  150  105  140  140
  2002  130   70   55   80
  2003  590  375  355  385
  ...
  </code></pre>
  
  <p>Preferred result.</p>
  
  <ul>
  <li>i = index number </li>
  <li>col = column number (Key2 = 1, Key3 = 2, etc.) </li>
  <li>p = random number  </li>
  <li><p>value = value calculated with the column number and p</p>
  
  <pre><code>year   i col         p     value
  2001   1   1 0.7481282 4.0150810
  2001   2   1 0.8449366 2.0735090
  2001 ...   1 0.1906882 0.9534411
  2001 150   1 0.8030162 3.7406410
  2001   1   2 0.4147019 4.2246831
  2001   2   2 0.3716995 1.8584977
  2001 ...   2 0.5280272 2.6401361
  2001 105   2 0.8030162 3.7406410
  2001   1   3 0.7651376 3.8256881
  2001   2   3 0.2298984 1.1494923
  2001 ...   3 0.5607825 2.8039128
  2001 140   3 0.7222644 3.6113222
  etc.
  
  2002   1   1 0.1796613 0.8983065
  2002   2   1 0.6390833 3.1954165
  2002 ...   1 0.5280272 2.6401367
  2002 130   1 0.4238842 2.1194210
  2002   1   2 0.7651376 3.8256889
  2002   2   2 0.2298984 1.1494928
  2002 ...   2 0.5607825 2.8039125
  2002  70   2 0.7222644 3.6113227
  2002   1   3 0.7512801 3.7564000
  2002   2   3 0.4484248 2.2421240
  2002 ...   3 0.5662704 2.8313520
  2002  55   3 0.7685377 3.8426884
  etc.
  </code></pre></li>
  </ul>
  
  <p>I use the following code in R, but it is very slow with a large dataset.
  I tried to keep the use of loops to a minimum by using <code>rep()</code> but I still have to for-loops in the code.</p>
  
  <p>Is there a faster / more efficient way to do this is R? Using data.table? </p>
  
  <pre><code>val &lt;- c(); i &lt;- c(); cols &lt;- c(); p &lt;- c(); year &lt;- c()
  for (year in 1:10) {
    for (n in 2:25) {
        c &lt;- n-1
        pu &lt;- runif(dataset1[[year, n]])
        p &lt;- c(p, pu )
        tmp &lt;- (c-1)*5 + 5*pu
        val &lt;- c(val, tmp)
        ##
        i &lt;- c(i, 1:dataset1[[year, n]])
        cols &lt;- c(cols, rep(c, dataset1[[year, n]]) )
        year &lt;- c(year, rep(dataset1[[year,1]], dataset1[[year, n]]) )
    }
  }
  res.df &lt;- data.frame(year=year, i=i, cols=cols, p=p, val=val)
  res.df &lt;- setDT(res.df)
  </code></pre>