I'm not sure what you are trying to achieve, but I think this can be a good starting point:
files <- list.files("deleteme", full.names=TRUE, recursive=TRUE) names <- sapply(strsplit(files, "/", TRUE), "[", 2) x <- lapply(files, function(f) { out <- read.csv(f) out$city <- strsplit(f, "/", TRUE)[[1]][2] out }) y <- do.call("rbind", x) Mikhail > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Ista Zahn > Sent: Friday, March 11, 2011 10:53 AM > To: r-help@r-project.org > Subject: [R] Any existing functions for reading and extracting data > from path names? > > Hi helpeRs, > > I have inherited a set of data files that use the file system as a > sort of poor man's database, i.e., the data files are nested in > directories that indicate which city they come from. For example: > > dir.create("deleteme") > for(i in paste("deleteme", c("New York", "Los Angeles"), sep="/")) { > dir.create(i) > for(j in paste("data", 1:2, ".csv", sep="")) { > write.csv(data.frame(x=1:10), file=paste(i, j, sep="/")) > } > } > > list.files("deleteme", recursive=TRUE) > > What I want to end up with is > > x city wave > 1 New York 1 > 1 Los Angeles 1 > 1 New York 2 > 1 Los Angeles 2 > > I've started writting a simple function to do this, but it seems like > a common situation and I'm wondering if there are any packages or > functions that might make this easier. > > Thanks! > Ista > -- > Ista Zahn > Graduate student > University of Rochester > Department of Clinical and Social Psychology > http://yourpsyche.org > > ______________________________________________ > 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. ______________________________________________ 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.