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="") ) } Edmond
simu1 <- matrix(0,3,3) simu2 <- matrix(0,3,3) simu3 <- matrix(0,3,3) simu4 <- matrix(0,3,3) ## save to files write.table(simu1,"simu1.dat") write.table(simu2,"simu2.dat") write.table(simu3,"simu3.dat") write.table(simu4,"simu4.dat") ## read from files into a list called simu simu <- list(NULL) for (i in 1:4) { simu[[i]] <- read.table( paste("simu",i,".dat",sep="") ) } simu[[1]] On Wed, 5 Mar 2003, Edmond Ng wrote:> 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="") ) > } > > > Edmond > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
There's no bold face in plain text. I assume it's the line inside the loop that's giving you trouble. Two approaches: 1. for(i in 1:4) assign(paste("simu", i, sep=""), read.table(...)) 2. simu <- vector(4, mode="list") for(i in 1:4) simu[[i]] <- read.table(...) Andy> -----Original Message----- > From: Edmond Ng [mailto:Edmond.Ng at lshtm.ac.uk] > Sent: Wednesday, March 05, 2003 2:26 PM > To: r-help at stat.math.ethz.ch > Subject: [R] reading in tab delimited data in a loop > > > 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="") ) > } > > > Edmond > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
"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=""))) }