[R] read multiple large files into one dataframe

Paul Hiemstra p.hiemstra at geo.uu.nl
Mon Jan 25 11:43:35 CET 2010


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))

you replace the read.table function by a custom function:

listOfFiles <- list.files(pattern= ".txt")
d <- do.call(rbind, lapply(listOfFiles, function(fname) {
       dum = read.table(fname)
       dum$which_file = fname
       return(dum)
    }))

Now d has an additional column identifying which filename it originally  
belonged to.

cheers,
Paul

-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul



More information about the R-help mailing list