[R] how to get a value from a list (using paste function)?
William Dunlap
wdunlap at tibco.com
Thu Dec 27 18:01:38 CET 2012
> Can you please elaborate. What are the negatives about the method
Here are a few examples of eval(parse(text=paste())) failing:
> cvtest <- list("Test-1"=1, Bozo=2, "Joe's Test"=3)
>
> lambda.rule <- "Test-1"
> eval(parse(text=paste0("cvtest$", lambda.rule))) # want 1
[1] 0
>
> lambda.rule <- "Joe's Test"
> eval(parse(text=paste0("cvtest$", lambda.rule))) # want 3
Error in parse(text = paste0("cvtest$", lambda.rule)) :
<text>:1:11: unexpected INCOMPLETE_STRING
1: cvtest$Joe's Test
^
and here is an example of "$" giving a suboptimal result:
> cvtest$B # want NULL (there is no component named "B")
[1] 2
"[[" gives the correct result in all cases:
> lambda.rule <- "Test-1"
> cvtest[[lambda.rule]]
[1] 1
> lambda.rule <- "B"
> cvtest[[lambda.rule]]
NULL
> lambda.rule <- "Bozo"
> cvtest[[lambda.rule]]
[1] 2
> lambda.rule <- "Joe's Test"
> cvtest[[lambda.rule]]
[1] 3
Also, I find it hard to read code involving eval(parse(text=paste(...))).
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Heramb Gadgil
> Sent: Thursday, December 27, 2012 2:44 AM
> To: Uwe Ligges
> Cc: Thomas Stewart; r-help
> Subject: Re: [R] how to get a value from a list (using paste function)?
>
> I am not sure why "Never ever!"
>
> Can you please elaborate. What are the negatives about the method
>
> Warm Regards,
> Heramb M. Gadgil
>
>
> On Thu, Dec 27, 2012 at 3:50 PM, Uwe Ligges <ligges at statistik.tu-dortmund.de
> > wrote:
>
> >
> >
> > On 27.12.2012 08:09, Heramb Gadgil wrote:
>
> >> eval(parse(text=paste0("**cvtest$",lambda.rule)))
> >>
> >
> > No, never ever!
> >
> > There is an R idiom made for it:
> >
> > cvtest[[lambda.rule]]
> >
> > Uwe Ligges
> >
> >
> >
> >
> >
> >
> >
> > I hope this works.
> >>
> >> On Wed, Dec 19, 2012 at 12:57 AM, Thomas Stewart
> >> <tgs.public.mail at gmail.com>**wrote:
> >>
> >> Soyeon-
> >>>
> >>> A possible solution:
> >>>
> >>> get(lambda.rule,envir=**list2env(cvtest))
> >>>
> >>>
> >>> On Tue, Dec 18, 2012 at 12:34 PM, Soyeon Kim <yunni0731 at gmail.com>
> >>> wrote:
> >>>
> >>> Dear my R friends,
> >>>>
> >>>> I want to get a number from a list using paste function.
> >>>> In my example,
> >>>> lambda.rule <- "lambda.1se"
> >>>> cvtest is a list (result from cv.glmnet)
> >>>> and
> >>>> cvtest$lambda.1se
> >>>> [1] 1.308973
> >>>>
> >>>> I want to call the value using paste function.
> >>>> I used get function but there was an error.
> >>>> test <- get(paste("cvtest$",lambda.**rule, sep=""))
> >>>> Error in get(paste("cvtest$", lambda.rule, sep = "")) :
> >>>> object 'cvtest$lambda.1se' not found
> >>>>
> >>>> Do you guys know how to solve this issue?
> >>>>
> >>>> Thank you so much in advance and merry Christmas!
> >>>>
> >>>> Soyeon
> >>>>
> >>>> ______________________________**________________
> >>>> R-help at r-project.org mailing list
> >>>> https://stat.ethz.ch/mailman/**listinfo/r-
> help<https://stat.ethz.ch/mailman/listinfo/r-help>
> >>>> PLEASE do read the posting guide
> >>>> http://www.R-project.org/**posting-guide.html<http://www.R-
> project.org/posting-guide.html>
> >>>> and provide commented, minimal, self-contained, reproducible code.
> >>>>
> >>>>
> >>>>
> >>>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________**________________
> >>> R-help at r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/**listinfo/r-
> help<https://stat.ethz.ch/mailman/listinfo/r-help>
> >>> PLEASE do read the posting guide
> >>> http://www.R-project.org/**posting-guide.html<http://www.R-
> project.org/posting-guide.html>
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >>>
> >> [[alternative HTML version deleted]]
> >>
> >> ______________________________**________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/**listinfo/r-
> help<https://stat.ethz.ch/mailman/listinfo/r-help>
> >> PLEASE do read the posting guide http://www.R-project.org/**
> >> posting-guide.html <http://www.R-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list