Re: [R] variables with dynamic names?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 24 Oct 2006 - 12:32:58 GMT

Here is a third way to do the loop:

rows <- match(substring(filenames, 1, 1), LETTERS) cols <- as.numeric(substring(filenames, 2, 3)) m[cbind(rows, cols)] <- sapply(DFs, F)

On 10/24/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> On 10/24/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > Try something like this (untested):
> >
> > # replace with your function that inputs a data frame and outputs a number
> > F <- function(x) length(x)
> > # replace with setwd to appropriate path
> > setwd("/")
> >
> > filenames <- dir(pattern = "[.]csv$")
> > # DFs is a list of data frames; names(DFs) will be the filenames
> > DFs <- sapply(filenames, read.csv, simplify = FALSE)
> >
> > m <- matrix(0, 26, 12)
> > rownames(m) <- LETTERS
> >
> > for(nam in filenames) {
> > row <- substring(nam, 1, 1)
> > col <- as.numeric(substring(nam, 2, 3))
> > m[row, col] <- F(DFs[[nam]])
> > }
> >
> > The for loop could alternately be done like this:
> >
> > library(gsubfn)
> > f <- function(filename, row, col) m[row, as.numeric(col)] <<- F(DFs[[filename]])
>
> The regular expression should be "^(.)(..).*"
>
> > junk <- strapply(filenames, "^(.)(..)", f)
> >
> >
> > On 10/24/06, Antje <niederlein-rstat@yahoo.de> wrote:
> > > Hello,
> > >
> > > I have the following problem. I have a set of CSV files and they are
> > > named for a special position in a matrix (e.g. "A01.csv", "F06.csv",
> > > "H11.csv")
> > >
> > > Now, I would like to read in the data of all these files and eveluate
> > > something for each set (and write it at the position in a result matrix).
> > > How can I realize this with R?
> > >
> > > Ciao,
> > > Antje
> > >
> > > ______________________________________________
> > > R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 Tue Oct 24 22:37:50 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 24 Oct 2006 - 14:30:13 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.