Looping a Function Over Multiple Data Files - Mindy Morales

From Gleon Fellowship
Jump to: navigation, search

Usefulness of skill

  • DOM fluorescence spectroscopy generates many large matrix files
  • Files require multiple corrections, calculations, and extraction of wavelength regions to provide useful information about DOM pool
  • Usually done in MATLAB; open source code has not been developed for R
  • Technique is not exclusive to EEM data and can be applied to any analysis that requires applying the same operation to multiple files.

Challenges

Working directory should only contain files of specified extension that you want to analyze

Sample code

In this example, a humification index (HIX, with corrections from Ohno 2008) is calculated from multiple .csv files

Define function
HIX<-function(low,high){sum(high)/(sum(high)+sum(low))}
Batch import csv files from wd
hixfiles <- list.files(pattern = "csv")
Create NULL vector to drop data into
hix_o <- NULL
Loop calculates HIX across all files in wd
for (i in 1:length(hixfiles)){
   tmp <-  read.csv(hixfiles[i])
   hix_o[i] = HIX(tmp$low, tmp$high)
}
Define vector to store results
results = data.frame(hixfiles, hix_o)
print(results)
Export to wd
write.csv(hix_o,file="hix.csv")


Yandex.Metrica