Soyeon Kim
2012-Dec-18 17:34 UTC
[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
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.
Thomas Stewart
2012-Dec-18 19:27 UTC
[R] how to get a value from a list (using paste function)?
Soyeon- A possible solution: get(lambda.rule,envir=list2env(cvtest)) On Tue, Dec 18, 2012 at 12:34 PM, Soyeon Kim <yunni0731@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@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. > > >[[alternative HTML version deleted]]
Seemingly Similar Threads
- How to see a R code from a package?
- Opposite of paste function
- How to change leaf color by group in hclust plot or how to install A2R package in windows?
- how to seperate " "? or how to do regression on each variable when I have multiple variables?
- prediction error in ROCR package when sampled y consists of only one class