[R] Looping of read.table and assignment

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 23 17:58:49 CET 2009


See:

https://stat.ethz.ch/pipermail/r-help/2009-March/192422.html

On Mon, Mar 23, 2009 at 12:52 PM, Steve Murray <smurray444 at hotmail.com> wrote:
>
> Dear all,
>
> I am trying to read in and assign data from 50 tables in an automated fashion. I have the following code, which I created with the help of textbooks and the internet, but it only seems to read in the final data file over and over again. For example, when I type:> table_1951  I get the same values in the table as when I type> table_2000  despite the values in the source tables being different:
>
>
> year <- 1951:2000
> filelist <- paste("C:\\Documents and Settings\\Data\\table_",year,".txt", sep="")
> filelist
> # Code seems to operate successfully up to this point
>
> for (i in filelist) {
>   for (iyear in 1951:2000) {
>          assign(paste("table_",iyear, sep=""),read.table(file=i, header=TRUE, sep=","))
>          noquote(paste("LOADED FILE:",paste("table_",iyear, sep=""),sep=" "))
>                     }
>                     }
>
>
> Can anyone see what I've done wrong here?
>
> And just as an aside, as you can see, I've inserted the 'noquote' line so that when the code is running I should be able to see each file being read in - mainly as a 'checker'. Should this work as anticipated, with each line being displayed with its corresponding table number after it's been read in?
>
> Many thanks for any help offered,
>
> Steve
>
>
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> ______________________________________________
> 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