2

I cannot seem to get the following to work

directory <- "./"
files.15x16 <- c("15x16-70d.out", "15x16-71d.out")
data.15x16<-rbind( lapply( as.array(paste(directory, files.15x16, sep="")), FUN=read.csv, sep=" ", header=F) )

What it should be doing is pretty straightforward - I have a directory name, some file names, and actual files of data. I paste the directory and file names together, read the data from the files in, and then rbind them all together into a single chunk of data.

Except the result of the lapply has the data in [[]] - i.e., accessing it occurs via a[[1]], a[[2]], etc which rbind doesn't seem to accept.

Suggestions?

Carl
  • 7,538
  • 1
  • 40
  • 64
  • Take a look on http://stackoverflow.com/questions/2104483/how-to-read-table-multiple-files-into-a-single-table-in-r – Marek May 25 '10 at 19:20
  • And some "not-exactly-the-same": http://stackoverflow.com/questions/2209258/merge-several-data-frames-into-one-data-frame-with-a-loop, http://stackoverflow.com/questions/1562124/merge-many-data-frames-from-csv-files – Marek May 25 '10 at 19:24

1 Answers1

13

Use do.call:

data.15x16 <-  do.call(rbind, lapply(paste(directory, files.15x16, sep=""), 
                                      FUN=read.csv, sep=" ", header=F)) 

You also don't need the as.array - it does not really do anything here.

Aniko
  • 18,516
  • 4
  • 48
  • 45
  • brilliant, thanks; as.array was a leftover from using apply instead of lapply - apply was complaining about dim(X) not being positive – Carl May 24 '10 at 13:28