Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff051001250558o4ebd5059s8e265ba50f456efb@mail.gmail.com>
Date: 2010-01-25T13:58:21Z
From: Hadley Wickham
Subject: read multiple large files into one dataframe
In-Reply-To: <4B5D75D7.2010407@geo.uu.nl>

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/