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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to