Dear R-help, I am having quite a difficult time coming up with what I want to do involving named lists. I have a list of logical expressions, and I would really like it if the "names" of the components of the list were identical to the corresponding logical expression. So, as an example: df.example <- data.frame(a = 1:10, b = rnorm(10, 5)) list.example <- list(df.example$a > 7, df.example$b < 4) Now what I'd really like is to name the components, and get the results of the following line without having to specify the right-hand side individually for each component: names(list.example) <- c("df.example$a > 7", "df.example$b < 4") Any ideas? Best Regards, Erik Iverson
Try this:> s <- c("df.example$a > 7", "df.example$b < 4") > sapply(s, function(x) eval(parse(text = x)))df.example$a > 7 df.example$b < 4 [1,] FALSE TRUE [2,] FALSE TRUE [3,] FALSE FALSE [4,] FALSE FALSE [5,] FALSE FALSE [6,] FALSE FALSE [7,] FALSE TRUE [8,] TRUE FALSE [9,] TRUE FALSE [10,] TRUE TRUE On Thu, Jul 16, 2009 at 4:44 PM, Erik Iverson<eiverson at nmdp.org> wrote:> Dear R-help, > > I am having quite a difficult time coming up with what I want to do involving named lists. > > I have a list of logical expressions, and I would really like it if the "names" of the components of the list were identical to the corresponding logical expression. > > So, as an example: > > df.example <- data.frame(a = 1:10, b = rnorm(10, 5)) > > list.example <- list(df.example$a > 7, > ? ? ? ? ? ? ? ? ? ? df.example$b < 4) > > Now what I'd really like is to name the components, and get the results of the following line without having to specify the right-hand side individually for each component: > > names(list.example) <- c("df.example$a > 7", "df.example$b < 4") > > > Any ideas? > > Best Regards, > Erik Iverson > > ______________________________________________ > 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. >
On Thu, Jul 16, 2009 at 4:44 PM, Erik Iverson<eiverson at nmdp.org> wrote:> I have a list of logical expressions, and I would really like it if the "names" of the components of the list were identical to the corresponding logical expression. > > So, as an example: > > df.example <- data.frame(a = 1:10, b = rnorm(10, 5)) > > list.example <- list(df.example$a > 7, > ? ? ? ? ? ? ? ? ? ? df.example$b < 4) > > Now what I'd really like is to name the components, and get the results of the following line without having to specify the right-hand side individually for each component: > > names(list.example) <- c("df.example$a > 7", "df.example$b < 4")Something like this, perhaps?:> listx <- function(...) structure(list(...),names=tail(as.list(substitute(c(...))),-1)) > list.example <- list(df.example$a > 7, df.example$b < 4) > listx(df.example$a > 7, df.example$b < 4)$`df.example$a > 7` [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE $`df.example$b < 4` [1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE