I have two directories, each with many files in them. The files in each directory have the same name. What I'd like to do is apply a function (for instance a correlation, and extract the estimate) on dir1/file1 to dir2/file1, repeat this over all files which match in name, and store the result as a data frame.
I'm trying something like this:
f1 = list.files("path1", "*abc.csv")
f2 = list.files("path2", "*abc.csv")
for (i in 1:length(f1)) {
tmp <- as.matrix(read.csv(f1[i], header=FALSE))
tmp2 <- as.matrix(read.csv(f2[i], header=FALSE))
c = cor.test(tmp,tmp2)
lst[[f1[i]]] <- c$estimate
}
But I'm a little stuck due to the matching filenames and also thinking that apply
plus a match
call might be a better choice. I've searched and found solutions on dealing with importing and applying a function to multiple files, but not when importing two batches and the files have identical names.