Re: [R] Any existing functions for reading and extracting data from path names?

From: Ista Zahn <izahn_at_psych.rochester.edu>
Date: Sat, 12 Mar 2011 02:50:19 +0000

Thanks Mikhail. I've been doing something very similar to your example below, I was just wondering if anyone had packaged functions for this task.

Thanks again,
Ista
On Sat, Mar 12, 2011 at 12:39 AM, Mikhail Titov <mlt_at_gmx.us> wrote:
> 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-bounces_at_r-project.org [mailto:r-help-bounces_at_r-
>> project.org] On Behalf Of Ista Zahn
>> Sent: Friday, March 11, 2011 10:53 AM
>> To: r-help_at_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_at_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.
>
>

-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

______________________________________________
R-help_at_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.
Received on Sat 12 Mar 2011 - 03:34:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 12 Mar 2011 - 03:40:21 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive