[R] reading in tab delimited data in a loop

Douglas Bates bates at stat.wisc.edu
Wed Mar 5 21:52:13 CET 2003


Douglas Bates <bates at cs.wisc.edu> writes:

> "Edmond Ng" <Edmond.Ng at lshtm.ac.uk> writes:
> 
> > Dear all, 
> > 
> > I need to read in 4 sets of tab delimited data in a  loop. The 4 data sets are called "simu1.dat", "simu2.dat" and so on. I know what I need on the righthand side of the read.table expression but I can't the left hand side of it to work (see the line in bold below). Can you kindly help? Many thanks. 
> > 
> > simu1 <- matrix(0,30,3)
> > simu2 <- matrix(0,30,3)
> > simu3 <- matrix(0,30,3)
> > simu4 <- matrix(0,30,3)
> > 
> > for (i in 1:4) {
> > simu[i] <- read.table( paste("simu",i,".dat",sep="") )
> > }
> 
> Use paste to generate the name and assign to do the assignment
> 
> for (i in 1:4) {
>   assign(paste('simu', i), read.table( paste("simu",i,".dat",sep="")))
> }

Andy Liaw's answer got back to me before mine did and I see that he
remembered to use paste('simu', i, sep='').  Andy's version is
correct - mine would generate names like 'simu 1'.



More information about the R-help mailing list