title: R: problems constructing nested for-loop and saving results
body: <p>This is a question which is based on a question I asked before: <a href="https://stackoverflow.com/questions/17074277/read-many-files-compute-something-save-results-in-dataframe-r">Read many files, compute something, save results in dataframe (R)</a> .</p>
  
  <p>Still I have around 100 files with data from a simulation. Topic is the degradation of two substances, so I have 3 relevant columns: time (in minutes), relative concentration of substance 1 (Sub1, from 1.0 to zero) and relative conc. of substance 2 (Sub2).</p>
  
  <p>This works fine to determine certain degradation levels, which I have to enter manually:</p>
  
  <pre><code>z &lt;- 0.25 #Abbaugrad
  a &lt;- 1-z
  
  setwd("H:/...")
  names&lt;-dir(pattern="Data.txt")
  result &lt;- data.frame( matchs1 = numeric(length(names)) , matchs2 =    numeric(length(names)) )
  for (i in 1:length(names)){
    data &lt;- read.table(file=names[i],header=T)
    matchs1 &lt;- with( data, Time[abs(Sub1-a)==min(abs(Sub1-a))] )  
    matchs2 &lt;- with( data , Time[abs(Sub2-a)==min(abs(Sub2-a))] )
    result[i,] &lt;- cbind(matchs1,matchs2) 
    }
  </code></pre>
  
  <p>Now I wanted to automate the process so I don't have to enter degradation levels but the results are computed in a nested for-loop.</p>
  
  <p>First I thought it might be better for future ideas to work with a datalist, e. g. for plotting some data from this files, so I did:</p>
  
  <pre><code>filelist &lt;- list.files(pattern = "Data.txt")
  datalist &lt;- lapply(filelist, function(xx)read.table(xx,header=T))
  </code></pre>
  
  <p>This worked fine to plot some runs from the simulation.</p>
  
  <pre><code>plot(datalist[[1]][,3], type="l",col="red")
  for (i in 1:10){
     lines(datalist[[i]][,3],col="red")  
     }
  </code></pre>
  
  <p>The cause for trying to construct the following loop is that I want to find out the mean run out of the 100. I think I have to find out the time when 1 % to 100 % are degraded for each run and then calculate the mean of each level.</p>
  
  <p>Now my simple try for just one substance:</p>
  
  <pre><code>test &lt;- data.frame(matrix(ncol=99, nrow = 100) ) #100 runs, 99 levels
  for (i in 1:length(datalist)){
    for (j in seq(0.01,0.99,by=0.01)){
     matchs1 &lt;- with( datalist[[i]], Time[abs(Sub1-j)==min(abs(Sub1-j))] )  
     level &lt;- paste('l',j,sep="")
     test[j,] &lt;- cbind(matchs1,level)
     }}
  </code></pre>
  
  <p>I wanted to construct a new dataframe where the result for each level (from 0.01 to 1) is saved in a single column. Then I could calculate the mean for each coloumn an rbind it. But I fail already to compute the data frame.</p>
  
  <p>Maybe there is also an easier way to get the mean of different time-series.</p>
  
  <p>I would really appreciate any help. </p>
  
  <p>Edit:
  It's hard for me to provide sample data, because I have got 100 files. In each one of them there are three relevant columns: Time, Sub1, Sub2. You can imagine it like that:</p>
  
  <pre><code>Time    Sub1    Sub2
  0       1.0000  1.0000
  60      0.9511  0.9888
  120     0.9022  0.9555 
  ...
  24000   0.0101  0.0891
  </code></pre>
  
  <p>I want to compute the mean run, so for each of the hundred files I have to calculate the time, when 0.01, 0.02, 0.03 ... 0.97, 0.98, 0.99 are degraded. Then I want to calculate the mean of i. e. all times, where 0.01 are degraded. The same for 0.02, 0.03 and so on. In the end I should have 99 times in ascending order.</p>
  
  <p>Is it possible to attach files? I found no option to do that.</p>
  
  <p>Edit 2:</p>
  
  <p>The output of dput for the datalist obtained by</p>
  
  <pre><code>names &lt;- dir(pattern="Data.txt")
  datalist &lt;- lapply(names, read.table, header=TRUE)
  </code></pre>
  
  <p>is:</p>
  
  <pre><code>&gt; dput(head(datalist[[1]]))
  structure(list(Time = c(0L, 60L, 120L, 180L, 240L, 300L), Sub0_Abs = c(1.49899, 
  1.49568, 1.49544, 1.49533, 1.49521, 1.49507), Sub0_Rel = c(1, 
  0.9978, 0.9976, 0.9976, 0.9975, 0.9974), Sub1_Abs = c(1.49899, 
  1.49899, 1.49899, 1.49899, 1.49899, 1.49899), Sub1_Rel = c(1, 
  1, 1, 1, 1, 1), TP = c(0.15, 0.1579, 0.1584, 0.1586, 0.1589, 
  0.1592), TA = c(0.13, 0.16, 0.18, 0.18, 0.18, 0.22), Sub_Inflow = c(0, 
  0, 0, 0, 0, 0)), .Names = c("Time", "Sub0_Abs", "Sub0_Rel", "Sub1_Abs", 
  "Sub1_Rel", "TP", "TA", "Sub_Inflow"), row.names = c(NA, 6L), class = "data.frame")
  
  &gt; dput(head(datalist[[2]]))
  structure(list(Time = c(0L, 60L, 120L, 180L, 240L, 300L), Sub0_Abs = c(1.49899, 
  1.49284, 1.49234, 1.49207, 1.49176, 1.49141), Sub0_Rel = c(1, 
  0.9959, 0.9956, 0.9954, 0.9952, 0.9949), Sub1_Abs = c(1.49899, 
  1.49899, 1.49899, 1.49899, 1.49899, 1.49899), Sub1_Rel = c(1, 
  1, 1, 1, 1, 1), TP = c(0.15, 0.1647, 0.1658, 0.1664, 0.1671, 
  0.1679), TA = c(0.13, 0.21, 0.25, 0.25, 0.25, 0.37), Sub_Inflow = c(0, 
  0, 0, 0, 0, 0)), .Names = c("Time", "Sub0_Abs", "Sub0_Rel", "Sub1_Abs", 
  "Sub1_Rel", "TP", "TA", "Sub_Inflow"), row.names = c(NA, 6L), class = "data.frame")
  </code></pre>
  
  <p>The relevant columns are Time, Sub0_Rel and Sub1_Rel. In my earlier question I tried to shorten the names a little bit.</p>