title: How to store multiple vectors in elements of a list?
body: <p>I have generated a file that looks like this (here is an extract, roughly each one of two files with names having columns 1 and 2 in common are multiplied by a different parameter and the best fit, i.e. the lowest chi2, is returned in column 3 with the appropriate parameters in columns 4 and 5):</p>
  
  <pre><code>10 05 0.42 0.13 0.01   
  10 10 0.30 0.12 0.01  
  10 15 0.25 0.11 0.07  
  15 05 0.29 0.12 0.01  
  15 10 0.25 0.11 0.06  
  15 15 0.23 0.10 0.02  
  20 05 0.25 0.11 0.03  
  20 10 0.23 0.12 0.04  
  20 15 0.23 0.13 0.05  
  25 05 0.23 0.10 0.03  
  25 10 0.23 0.10 0.08  
  25 15 0.24 0.09 0.05  
  </code></pre>
  
  <p>I am starting/learning to use lists as my codes are really to slow using <code>for</code> loops (currently I am using 4 <code>for</code> loops so it's insanely long), and I don't know enough to re-write my optimisation code so it does not take 8 hours to work. So instead, to reorganise the output I was wondering if it would be possible to create a list, say <code>mytemplist</code>, that reads:</p>
  
  <pre><code>&gt; mytemplist  
  $5 
  [1] 10 15 20 25  
  [2] 0.42 0.29 0.25 0.23  
  [3] 0.13 0.12 0.11 0.10
  [4] 0.01 0.01 0.03 0.03  
  
  $10  
  [1] 10 15 20 25  
  [2] 0.30 0.25 0.23 0.23
  [3] 0.12 0.11 0.12 0.10  
  [4] 0.01 0.06 0.04 0.08  
  
  $15  
  [1] 10 15 20 25  
  [2] 0.25 0.23 0.23 0.24  
  [3] 0.11 0.10 0.13 0.09  
  [4] 0.07 0.02 0.05 0.05  
  </code></pre>
  
  <p>I have looked at questions about lists and I could only sort that out by creating lists within lists which is not helping here.</p>
  
  <hr>
  
  <p>EDIT:<br>
  the accepted answer replies to the specific question above, to answer @rawr post I join how the file is generated (it's not pretty, I am not using opt so far as I will evolve the code to optimise with bigger freedom around the data points):<br>
  note: typical file to read are 2-column file (just lists of numbers) and named a10b05s and a10b05t<br>
  dataname is also a 2-column file<br>
  in those 3 files the first column is the same and represents the pivots<br>
  need to find par[1] and par[2] such that par[1]*a10b05s + par[2]*a10b05t best fit data  </p>
  
  <pre><code>par &lt;- rep(NA, 2)
  pivot &lt;- read.table(dataname)[[1]]
  data2fit &lt;- read.table(dataname)[[2]]
  
  for (i in 1:10){
      vala &lt;- 10+5*(i-1)
      namei &lt;- paste("a", vala, sep="")
  
      for (j in 1:10){
          #creates a coordinates for storage
          cglobal &lt;- (i-1) * 10 + j           
  
          valb &lt;- 5+5*(j-1)
          namej1 &lt;- paste(namei, "b", valb, "s", sep="")
          namej2 &lt;- paste(namei, "b", valb, "t", sep="")
          infile1 &lt;- read.table(namej1)
          infile2 &lt;- read.table(namej2)
  
          # infile1 prominent wrt infile2 so first quick determination of par1 
          tempspace1 &lt;- seq(0.001, 0.009, 0.001)
          par1_s1 &lt;- c(tempspace1, tempspace1*10, tempspace1*100) 
          opt1_par1 &lt;- rep(NA, length(par1))
  
          # set a pivot for comparison at position named temppivot find par1 wrt temppivot
          for(k in 1:length(par1){
              opt1_par1[k] &lt;- abs(par1_s1[k]*infile1[[1]][temppivot] - data2fit[temppivot])
          }
  
          par[1] &lt;- par1_s1[match(min(opt1_par1)), opt1_par1]
  
          # set a space for a finer fit for par[1]
          par1_s2 &lt;- seq(par[1]-5*par[1]/10, par[1]+5*par[1]/10, par[1]/100)
  
          # set a space to fit par[2] note that there is an option in the code to choose btw 0.001-0.01, 0.01-0.1 etc.
          tempspace2 &lt;- seq(0.001, 0.009, 0.0001)
          par2 &lt;- c(tempspace2, tempspace2*10, tempspace2*100)
          chi2 &lt;- rep(NA, length(par1_s2)*length(par2))
          #data2fit 
  
          for(z in 1:length(par1_s2)){
              for(w in 1:length(par2)){
                  par[1] &lt;- par1_s2[z]
                  par[2] &lt;- par2[w]
                  thesum &lt;- rep(NA, length(pivot))
                      for(h in 1:length(pivot)){
                          c1 &lt;- pivot[h]
                          thesum[h] &lt;- par[1] * infile[[1]][c1] + par[2] * infile2[[1]][c1]
                      }
              c2 &lt;- (z-1) * length(par2) + w
              chi2[c2] &lt;- sum((thesum-data2fit)^2/thesum)
              }
          }
  
          whichbestfit &lt;- match(min(chi2), chi2)
          chi2min &lt;- min(chi2)
  
          localparfinder &lt;- function(x){
              temp1 &lt;- trunc(x/length(par2)) + 1
              temp2 &lt;- x - (temp1 -1) * length(par2)
              y &lt;- c(par1_s2[temp1], par2[temp2])
              }
  
          par &lt;- localparfinder(whichbestfit)
  
          # creates the table of the original post
          storage[cglobal,] &lt;- c(vala, valb, chi2min, par[1], par[2])
      }
  }
  
  
  write.table(storage, file=paste("storage_", format(Sys.time(), "%d%b_%H%M"), ".dat", sep="")
  </code></pre>