Skip to content
Back to formatted view

Raw Message

Message-ID: <D9C5E8C8-62EA-4291-9654-6842064BBD91@exeter.ac.uk>
Date: 2009-03-23T17:11:02Z
From: Baptiste Auguie
Subject: Looping of read.table and assignment
In-Reply-To: <BAY135-W13B49AFA7BD13FE1F6605588920@phx.gbl>

You need only one loop,

> year <- 1951:2000
> filelist <- paste("C:\\Documents and Settings\\Data\ 
> \table_",year,".txt", sep="")
> filelist

> for (ii in seq_along(year)) {
>          assign(paste("table_", year[ii], sep=""),
> 				read.table(file=ifile[ii], header=TRUE, sep=","))
> }
>


or better yet,

> d <- lapply( filelist, read.csv)
> names(d) <- paste(year)

> head(d["1952"])


(untested)

HTH,

baptiste

On 23 Mar 2009, at 16:52, Steve Murray 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.

_____________________________

Baptiste Augui?

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag