[R] how to get a value from a list (using paste function)?

arun smartpink111 at yahoo.com
Tue Dec 18 19:08:02 CET 2012


HI,

It guess ?get() does not work with $ elements.
x1 <- list(a=c(1,2), b = c(2,4), c=c(4,5,6))
#this works
 get(paste("x","1",sep=""))["a"]
#$a
#[1] 1 2
#or
 get(paste("x","1",sep=""))[[2]]
#[1] 2 4
A.K.






----- Original Message -----
From: Soyeon Kim <yunni0731 at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Tuesday, December 18, 2012 12:34 PM
Subject: [R] how to get a value from a list (using paste function)?

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
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