[R] syntax for batching rbind process

Toby Gass tobygass at warnercnr.colostate.edu
Wed Aug 18 21:59:22 CEST 2010


Thank you for the suggestions for the more efficient code.  The 
problem remains, however, that the final dataframe does not contain 
the correct values.  So, in the case of the code you suggested, 

imp <- lapply(test, read.csv)
do.call(rbind, imp)

imp does contain all the data from each dataframe, and the data from 
each csv can be accessed with a single bracket index, but the do.call 
does not work, possibly because rbind doesn't work on a list??  

Any additional suggestions will be happily tested.  I'm still 
figuring out how to create a functionally equivalent toy example.

Thank you.

Toby


On 18 Aug 2010 at 13:49, Erik Iverson wrote:

> 
> 
> Toby Gass wrote:
> > Dear helpeRs,
> > 
> > I am attempting to read in a series of csv files so I can bind them 
> > into one large dataframe.  I have written the following script:
> > 
> > test <- list.files(".", pattern = "csv")  #lline 1
> > imp <- list()                                    #line 2
> > for (i in 1:length(test)) {                    #line 3
> > imp[i] <- read.csv(test[i])                  #line 4
> > }                                                    #line 5
> > works <- do.call(rbind, imp)              #line 6
> > write.csv(works, "allmet.csv")           #line 7
> > 
> > This script has an error at line 4.  imp[i] contains only the value 
> > of the first element of test[i]; in other words, every element of 
> > imp[i] equals test[i] [1,1].  Otherwise, the script works.  Could 
> > someone please enlighten me as to the correct syntax for line 4?
> 
> Just use lapply instead of a loop, easier syntax.
> 
> imp <- lapply(test, read.csv)
> do.call(rbind, imp)
>



More information about the R-help mailing list