[R] read multiple large files into one dataframe

hadley wickham h.wickham at gmail.com
Mon Jan 25 14:58:21 CET 2010


On Mon, Jan 25, 2010 at 4:43 AM, Paul Hiemstra <p.hiemstra at geo.uu.nl> wrote:
> Brad Patrick Schneid wrote:
>>
>> ###  The following is very helpful ######### listOfFiles <-
>> list.files(pattern= ".txt") d <- do.call(rbind, lapply(listOfFiles,
>> read.table)) ###############################
>>
>> but what if each file contains information corresponding to a different
>> subject and I need to be able to tell where each row came from?  i.e.: I
>> need a new row
>
> a new column I presume, not a row
>>
>> that repeats the original filename for each observation of
>> the former respective files.
>>
>> Any ideas?
>>
>>
>
> listOfFiles <- list.files(pattern= ".txt")
> d <- do.call(rbind, lapply(listOfFiles, read.table))

Or use the plyr package:

listOfFiles <- list.files(pattern= ".txt")
names(listOfFiles) <- basename(listOfFiles)

d <- ldply(listOfFiles, read.table)

See http://had.co.nz/plyr for more info.

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list