[R] Looping of read.table and assignment
baptiste auguie
ba208 at exeter.ac.uk
Mon Mar 23 18:11:02 CET 2009
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
More information about the R-help
mailing list