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 Thanks for any help, Thomas Jackson
Hi Thomas,
Here are two suggestions which storage all the files in a list:
# Parameters
route <- "/Users/thomasjackson/Data/GEP&CO/GEP&CO"
lts <- LETTERS[1:12]
toread <-
paste(route,lts,"/HPLC_",lts,"12.csv",sep='')
# Reading in the data -- option 1
myfiles <- list()
for(i in 1:12) myfiles[i] <- read.csv(toread, header=T, sep=',')
names(myfiles) <- paste("G&P", lts, sep='')
myfiles
# Option 2 -- no for() loop
myfiles2 <- lapply(toread, read.csv, header=T, sep=',')
myfiles2
To access the first file just type either
myfiles[[1]]
or
myfiles2[[1]]
For further analysis using each file, the lapply (see ?lapply) function
might be useful.
HTH,
Jorge
On Thu, 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
>
> Thanks for any help,
> Thomas Jackson
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
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.
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