I want to load many files in the R. The names of the files are "Sim1.txt", " Sim2.txt", "Sim3.txt", "Sim4.txt", "Sim5.txt" and so on. Can I read them at one time? What should I do? I can give the same names in R. Thanks. For example:> tst=paste("Sim",1:20,".txt",sep="") # the file names > tst[1] "Sim1.txt" "Sim2.txt" "Sim3.txt" "Sim4.txt" "Sim5.txt" "Sim6.txt" [7] "Sim7.txt" "Sim8.txt" "Sim9.txt" "Sim10.txt" "Sim11.txt" "Sim12.txt" [13] "Sim13.txt" "Sim14.txt" "Sim15.txt" "Sim16.txt" "Sim17.txt" "Sim18.txt" [19] "Sim19.txt" "Sim20.txt"> data.name=paste("Sim",1:20,sep="") # the file names in R > data.name[1] "Sim1" "Sim2" "Sim3" "Sim4" "Sim5" "Sim6" "Sim7" "Sim8" "Sim9" [10] "Sim10" "Sim11" "Sim12" "Sim13" "Sim14" "Sim15" "Sim16" "Sim17" "Sim18" [19] "Sim19" "Sim20" [[alternative HTML version deleted]]
On 14-Jul-07, at 6:16 PM, Zhang Jian wrote:> I want to load many files in the R. The names of the files are > "Sim1.txt", " > Sim2.txt", "Sim3.txt", "Sim4.txt", "Sim5.txt" and so on. > Can I read them at one time? What should I do? I can give the same > names in > R. > Thanks. > > For example: >> tst=paste("Sim",1:20,".txt",sep="") # the file names >> tst > [1] "Sim1.txt" "Sim2.txt" "Sim3.txt" "Sim4.txt" "Sim5.txt" > "Sim6.txt" > [7] "Sim7.txt" "Sim8.txt" "Sim9.txt" "Sim10.txt" "Sim11.txt" > "Sim12.txt" > [13] "Sim13.txt" "Sim14.txt" "Sim15.txt" "Sim16.txt" "Sim17.txt" > "Sim18.txt" > [19] "Sim19.txt" "Sim20.txt" > >> data.name=paste("Sim",1:20,sep="") # the file names in R >> data.name > [1] "Sim1" "Sim2" "Sim3" "Sim4" "Sim5" "Sim6" "Sim7" > "Sim8" "Sim9" > [10] "Sim10" "Sim11" "Sim12" "Sim13" "Sim14" "Sim15" "Sim16" > "Sim17" "Sim18" > [19] "Sim19" "Sim20"you could read each data file as a named element into a single list data=list(NULL) for(i in 1:length(tst)){ data[[i]]=read.table(tst[i]) names(data)[i]=data.name[i] } if you want to refer to Sim1, type data$"Sim1"
This should do it: allData <- sapply(paste("Sim",1:20,sep=""), function(.x) read.table(paste(.x,"txt",sep=".")), simplify=FALSE) see ?read.table for specification of delimiters, etc. allData will be a list, and you can access the contents of each file by any of the following commands: allData[[2]] allData[["Sim2"]] allData$Sim2 --- Zhang Jian <jzhang1982 at gmail.com> wrote:> I want to load many files in the R. The names of the files are "Sim1.txt", > " > Sim2.txt", "Sim3.txt", "Sim4.txt", "Sim5.txt" and so on. > Can I read them at one time? What should I do? I can give the same names in > R. > Thanks. > > For example: > > tst=paste("Sim",1:20,".txt",sep="") # the file names > > tst > [1] "Sim1.txt" "Sim2.txt" "Sim3.txt" "Sim4.txt" "Sim5.txt" "Sim6.txt" > [7] "Sim7.txt" "Sim8.txt" "Sim9.txt" "Sim10.txt" "Sim11.txt" > "Sim12.txt" > [13] "Sim13.txt" "Sim14.txt" "Sim15.txt" "Sim16.txt" "Sim17.txt" > "Sim18.txt" > [19] "Sim19.txt" "Sim20.txt" > > > data.name=paste("Sim",1:20,sep="") # the file names in R > > data.name > [1] "Sim1" "Sim2" "Sim3" "Sim4" "Sim5" "Sim6" "Sim7" "Sim8" "Sim9" > [10] "Sim10" "Sim11" "Sim12" "Sim13" "Sim14" "Sim15" "Sim16" "Sim17" > "Sim18" > [19] "Sim19" "Sim20" > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Storing them as elements of list is fine, but if you want them as individual objects, then something like this (not tested): for (i in seq( length(tst) ) ) { tmp <- read.table( tst[i] ) ## or read.delim() or read.csv(), as necessary assign( data.name[i], tmp ) } -Don At 3:16 PM -0600 7/14/07, Zhang Jian wrote:>I want to load many files in the R. The names of the files are "Sim1.txt", " >Sim2.txt", "Sim3.txt", "Sim4.txt", "Sim5.txt" and so on. >Can I read them at one time? What should I do? I can give the same names in >R. >Thanks. > >For example: >> tst=paste("Sim",1:20,".txt",sep="") # the file names >> tst > [1] "Sim1.txt" "Sim2.txt" "Sim3.txt" "Sim4.txt" "Sim5.txt" "Sim6.txt" > [7] "Sim7.txt" "Sim8.txt" "Sim9.txt" "Sim10.txt" "Sim11.txt" "Sim12.txt" >[13] "Sim13.txt" "Sim14.txt" "Sim15.txt" "Sim16.txt" "Sim17.txt" "Sim18.txt" >[19] "Sim19.txt" "Sim20.txt" > >> data.name=paste("Sim",1:20,sep="") # the file names in R >> data.name > [1] "Sim1" "Sim2" "Sim3" "Sim4" "Sim5" "Sim6" "Sim7" "Sim8" "Sim9" >[10] "Sim10" "Sim11" "Sim12" "Sim13" "Sim14" "Sim15" "Sim16" "Sim17" "Sim18" >[19] "Sim19" "Sim20" > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062