[R] performing script on multiple files

Phil Spector spector at stat.berkeley.edu
Fri Sep 24 21:57:40 CEST 2010


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.
>



More information about the R-help mailing list