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

Greg Snow Greg.Snow at imail.org
Thu Nov 19 19:39:47 CET 2009


Try something like:

gp <- lapply(  paste(("/Users/thomasjackson/Data/GEP&CO/GEP&CO",LETTERS[1:12],"/HPLC_",LETTERS[1:12],"12.csv",sep=''),
	 	read.csv, header=TRUE, sep=',' )
names(gp) <- paste("GandP", LETTERS[1:12], sep='')

Now gp (or whatever you want to call it) will be a list with your 12 data files as elements.  You can analyze individual elements, or use lapply to perform the same analysis on each of them.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Thomas Jackson
> Sent: Thursday, November 19, 2009 10:29 AM
> To: r-help at r-project.org
> Subject: [R] Reading in a series of files using a for loop
> 
> 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],"/H
> PLC_",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
> 
> Thanks for any help,
> Thomas Jackson
> ______________________________________________
> 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