[R] Changing many csv files using apply?
Chang, Emily@OEHHA
emily.chang at oehha.ca.gov
Mon Jun 18 23:38:14 CEST 2012
Dear all,
I have many csv files whose contents I want to change a bit en masse. So far, I've written code that can change them in a for loop, like so:
# Subset of files in the folder I want to change
subset = "somestring"
# Retrieve list of files to change
filelist=list.files()
filelist = filelist[grep(subset, filelist)]
for(i in 1:length(filelist)){
setwd(readdir)
temp = read.csv(filelist[i],as.is = T, strip.white = T)
>whatever I want to do to temp
setwd(writedir)
write.table(temp, file = filelist[i], sep = ",", col.names=NA)
}
It's a little slow though, so I would like to get rid of the for loop but preserve its function. Would it be possible to use sapply() or something similar? Any insight would be appreciated!
Best regards,
Emily
More information about the R-help
mailing list