say that I have a script that uses read.csv to read a textfile in a certain directory, then transforming the data a bit and then spit out a new file in an output directory. Is it possible to make this universal so the same procedure is made for all files in a certain directory and also make it spit out files that changes names for each file that is made (so the same output.csv is not overwritten every time the script runs)?
performing script on multiple files
2 messages · Jonas Josefsson, Phil Spector
Jonas -
Here's an (untested) idea:
readwrite = function(file,outputdirectory){
dat = read.csv(file)
. . .
write.csv(dat,file = paste(outputdirectory,sub('\\.csv','.new',file),sep='/'))
}
sapply(list.files('.',pattern='\\.csv$'),readwrite,'/some/location')
It will read all the .csv files in the current directory, and write out
a file with the extension changed to .new in the directory /some/location .
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 24 Sep 2010, Jonas Josefsson wrote:
say that I have a script that uses read.csv to read a textfile in a certain directory, then transforming the data a bit and then spit out a new file in an output directory. Is it possible to make this universal so the same procedure is made for all files in a certain directory and also make it spit out files that changes names for each file that is made (so the same output.csv is not overwritten every time the script runs)?
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.