[R-sig-eco] how to use read.csv to import a series of files

Kingsford Jones kingsfordjones at gmail.com
Wed Dec 30 01:46:57 CET 2009


On Mon, Dec 28, 2009 at 8:15 AM, Zongshan Li <zongshan_li at yahoo.com> wrote:
> Dear the friends of R-Ecology:
>      I am a beginner for R language, and I have a small problem to define the names of the imported CSV files.
>      The codes as following:
> -------------------------------------
> clim.var<-c("cld","dtr","frs","pre","tmn","tmp","tmx","vap","wet")
> for(i in 1:9)
> {
> cell.name<-paste("F: /53479",clim.var[i],".csv",sep="")
> var.name<-paste(clim.var[i],"53479",sep="")
> var.name<-read.csv(cell.name,header=TRUE)
> }


Each iteration of your loop creates a charcter object of length one
called var.name, which is then overwritten by a data.frame, also
called var.name.  To assign an object to a character string see
?assign.  Better yet, preallocate a list and fill it using a loop.
This post should clarify:
http://tolstoy.newcastle.edu.au/R/e6/help/09/05/14233.html

hth,

Kingsford Jones




> -----------------------------
> During the for loop, nine CSV file will be imported into R and I want to define each csv file with a special name.
> All the codes run well, except for the last row.
> I am confused that how to use read.csv to import a series of files in the for loop?
>
> Any help is strongly appreciated,
> thank you in advance,
>
> ----------------------
> Zongshan, Li
> 2009-12-28
> Research Center for Eco-Environmental Sciences,Chinese Academy of Sciences
> Shuangqing Road, Haidian street, Beijing, 100085
> Tel: 86-10-13699145748
> QQ: 1141958023
>
> website: http://www.rcees.ac.cn/
>
>
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
>



More information about the R-sig-ecology mailing list