tags:

views:

121

answers:

3

I noticed I encounter this task quite often when programming in R, yet I don't think I implement it "pretty".

I get a list of file names, each containing a table or a simple vector. I want to read all the files into some construct (list of tables?) so I can later manipulate them in simple loops.

I know how to read each file into a table/vector, but I do not know how to put all these objects together in one structure (list?).

Anyway, I guess this is VERY routine so I'll be happy to hear about your tricks.

+2  A: 

Do all the files have the same # of columns? If so, I think this should work to put them all into one dataframe.

library(plyr)
x <- c(FILENAMES)
df <- ldply(x, read.table, sep = "\t", header = T)

If they don't have all the same columns, then use llply() instead

JoFrhwld
+1 sometimes they do. thanks!
David B
That will work even if the files don't have the same number of columns.
hadley
+1  A: 

Or, without plyr:

filenames <- c("file1.txt", "file2.txt", "file3.txt")
mydata <- array(list(NULL))

for (i in 1:length(filenames))
    {
    mydata[[i]] <- read.table(filenames[i])
    }
nico
+1 nice and simple!
David B
but better to pre-allocate the list, and might as well use the filenames: mydata <- vector("list", length(filenames)); names(mydata) <- filenames; ... mydata[[filenames[i]]] <- read.table(filenames[i])
mdsumner
@mdsumner: What's the advantage of preallocating? Also, in this way you can put the `read.table` statement in a `tryCatch` instruction, so if file does not exist/is corrupted etc, you end up with a NULL in `mydata` which can then be checked against later in the script
nico
it saves memory usage - otherwise the entire mydata list is recreated each time it is visited by an incremented [[i]]
mdsumner