Mean values from multiple csv to data frame
<p>After having searched for help in different threads on this topic, I still have not become wiser. Therefore: Here comes another question on looping through multiple data files...</p>

<p>OK. I have multiple CSV files in one folder containing 5 columns of data. The filenames are as follows:</p>

<p>Moist yyyymmdd hh_mm_ss.csv</p>

<p>I would like to create a script that reads processes the CSV-files one by one doing the following steps:</p>

<p>1) load file
2) check number of rows and exclude file if less than 3 registrations
3) calculate mean value of all measurements (=rows) for column 2
4) calculate mean value of all measurements (=rows) for column 4
5) output the filename timestamp, mean column 2 and mean column 4 to a data frame, </p>

<p>I have written the following function</p>

<pre><code>moist.each.mean &lt;- function() {
  library("tcltk")
  directory &lt;- tk_choose.dir("","Choose folder for Humidity data files")
  setwd(directory)
  filelist &lt;- list.files(path = directory)
  filetitles &lt;- regmatches(filelist, regexpr("[0-9].*[0-9]", filelist))
  mdf &lt;- data.frame(timestamp=character(), humidity=numeric(), temp=numeric())

  for(i in 1:length(filelist)){
    file.in[[i]] &lt;- read.csv(filelist[i], header=F)
    if (nrow(file.in[[i]]&lt;3)){
      print("discard")
    } else {
      newrow &lt;- c(filetitles[[i]], round(mean(file.in[[i]]$V2),1), round(mean(file.in[[i]]$V4),1))
      mdf &lt;- rbind(mdf, newrow)
    }
  } 
  names(mdf) &lt;- c("timestamp", "humidity", "temp")
}
</code></pre>

<p>but i keep getting an error:</p>

<pre><code>Error in `[[&lt;-.data.frame`(`*tmp*`, i, value = list(V1 = c(10519949L,  : 
  replacement has 18 rows, data has 17 
</code></pre>

<p>Any ideas?</p>

<p>Thx, kruemelprinz</p>