I have prepared a r script and trying to apply the same codes in multiple files currently working with 5 sample files (but trying to learn to work with files over 100) in th
To read a set of csv files into a data.frame, I often use ldply from the plyr package:
library(plyr)
all_data = ldply(list.files(pattern = "csv"), function(fname) {
dum = read.csv(fname)
dum$fname = fname # adds the filename it was read from as a column
return(dum)
})
If you need more specific things, you extend the function you call with ldply.
does this toy example do what you want?
# clean up:
rm(list = ls())
setwd(tempdir())
unlink(dir(tempdir()))
# create some files in tempdir:
a <- data.frame(x = 1:3, y = 4:6)
b <- data.frame(x = 10:13, y = 14:15)
write.csv(a, "file1.csv", row.names = F)
write.csv(b, "file2.csv", row.names = F)
# now read all files to list:
mycsv = dir(pattern=".csv")
n <- length(mycsv)
mylist <- vector("list", n)
for(i in 1:n) mylist[[i]] <- read.csv(mycsv[i])
# now change something in all dfs in list:
mylist <- lapply(mylist, function(x) {names(x) <- c("a", "b") ; return(x)})
# then save back dfs:
for(i in 1:n)
write.csv(file = paste("file", i, ".csv", sep = ""),
mylist[i], row.names = F)