Message-ID: <644e1f320903211449x7c0809e3rea635c55ad656c9@mail.gmail.com>
Date: 2009-03-21T21:49:25Z
From: jim holtman
Subject: macro in a loop
In-Reply-To: <5562ab3b0903211253y520bf292mf83432b3da8e0dc2@mail.gmail.com>
COnsider the use of a 'list':
dataYear <- lapply(c(1940, 1950, 1960), function(.file){
read.table(paste("c:/data/", .file, '.csv', sep=''), header=TRUE, sep=',')
})
The you can access your data:
dataYear[['1940']]
or
dataYear$"1940"
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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?