[R] How to read many files at one time?

Stephen Tucker brown_emu at yahoo.com
Sat Jul 14 23:58:55 CEST 2007


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.
>



More information about the R-help mailing list