[R] Combining many files into one

jim holtman jholtman at gmail.com
Tue Nov 4 03:36:03 CET 2008


The error message might mean that you have an empty file.  It is
probably better to use a list for reading in the data and then combine
them at the end.  "try" may also catch your errors:

all.dat <- lapply(list.files(mydir), function(.file){
    try(read.csv(.file))
})
all.dat <- do.call(rbind, all.dat)


On Mon, Nov 3, 2008 at 9:17 PM, t c <mudiver1200 at yahoo.com> wrote:
> I have a few hundred .csv files in a folder on my hard drive, and I would like to combine them all into one file.  They are all the same data, but the program that collected the data saved a file every 4 hours.  I can combine the files one at a time using rbind, but am having a problem automating it in a for loop.  The loop runs for a while, and finally gives an error message "no lines available in input".  I would appreciate any help you can give.  What I have so far is below.  Thanks!  Tim
>
>
> files<-list.files(mydir)                               #List of every file in the working directory
>
> for (i in 1:length(files))                             #Loops through files
>  {
>  dat <- read.csv(files[i], header=T)          #Reads one file at a time
>  all.dat<-rbind(dat)                                  #Binds the file just read to the end of all.dat
>  }
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list