[R] Reading in a series of files using a for loop

David Winsemius dwinsemius at comcast.net
Thu Nov 19 19:52:36 CET 2009


You need to look at the assign function:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f

(... and in addition to R FAQ 7.21 also perhaps read the rest of the R  
FAQ.)

There are a ton of very similar questions in the r-help archives, so  
you could also try

RSiteSearch("read.table paste")

-- David




On Nov 19, 2009, at 12:29 PM, Thomas Jackson wrote:

> Dear R Users,
>
> I am trying to read in a series of csv files which vary by the  
> letter on the end of he file name.  When I input what seems to be a  
> logical for loop I get an error message that doesn't make sense to me.
>
>> for(i in 1:12){ paste("G&P", LETTERS[i],sep='') <-read.csv(paste("/ 
>> Users/thomasjackson/Data/GEP&CO/GEP&CO",LETTERS[i],"/ 
>> HPLC_",LETTERS[i],"12.csv",sep=''), header=T, sep=',')}
>
>
> Error in paste("G&P", LETTERS[i], sep = "") <- read.csv(paste("/ 
> Users/thomasjackson/Data/GEP&CO/GEP&CO",  :
>  target of assignment expands to non-language object
>
>
> For example the first file name is HPLC_A12.csv in the folder GEP&COA

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list