[R] macro in a loop

Gabor Grothendieck ggrothendieck at gmail.com
Sat Mar 21 21:16:27 CET 2009


R does not directly support macros and I don't think that that is what
you meant.  Also we probably want to put the data frames in a list
so we can easily operate over all of them later.  (If these are time
series also see read.zoo in the zoo package.)

setwd("c:/data")
filenames <- paste(year, "csv", sep = ".")
DFlist <- sapply(filenames, read.csv, simplify = FALSE)

On Sat, Mar 21, 2009 at 3:53 PM, Le Wang <ruser.le at gmail.com> wrote:
> Hi there,
>
> Thanks for your time in advance.
>
> I am trying to read in multiple files. For example,
>
>  data.1940 <- read.table("c:/data/1940.csv",header=TRUE,sep=",")
>  data.1950 <- read.table("c:/data/1950.csv",header=TRUE,sep=",")
>  data.1960 <- read.table("c:/data/1960.csv",header=TRUE,sep=",")
>
> How can I write a loop to read the data? I was trying to use the following
>
>        year<-c(1940,1950,1960)
>
>        for (j in 1:3){
>
>                      data.year[j] <- read.table("c:/data/year[j]
> .csv",header=TRUE,sep=",")
>
>        }
>
> But it is obviously wrong, as the marco is not proctected.
>
> I have been googling around for a while but haven't succeeded in
> finding any solutions. Thanks again for your help.
>
> Le
>
> ______________________________________________
> 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