Hi, Let us suppose I have a list x = list () x $ name1 = 1 x $ name2 = 'a' in the work environment. Let us suppose that in the body of a function I want to acces to a component of x by using its name as argument of that function. How can this by done? For instance, I was expecting f = function ( name ) x $ name to output 1 ( that is, x $ name1 ) when I command f ( name1 ) or f ( 'name1' ), but that is not happening. Does anyone knows how I can fix f to give me x $ name, while the argument is still the name of the component of x? Thank you.
(no subject)
3 messages · Francisco J Molina, Scionforbai, Uwe Ligges
Just read some introductory R tutorial...
x = list () x $ name1 = 1 x $ name2 = 'a' x
$name1 [1] 1 $name2 [1] "a"
name <- "name1" x[name]
$name1 [1] 1
x[[name]]
[1] 1
name <- "name2" x[name]
$name2 [1] "a"
x[[name]]
[1] "a" Bye, Scionforbai
Francisco J Molina wrote:
Hi, Let us suppose I have a list x = list () x $ name1 = 1 x $ name2 = 'a' in the work environment. Let us suppose that in the body of a function I want to acces to a component of x by using its name as argument of that function. How can this by done? For instance, I was expecting f = function ( name ) x $ name
f <- function(name) x[[name]]
Please read the docs such as help("$").
Uwe Ligges
to output 1 ( that is, x $ name1 ) when I command f ( name1 ) or f ( 'name1' ), but that is not happening. Does anyone knows how I can fix f to give me x $ name, while the argument is still the name of the component of x? Thank you.
______________________________________________ R-help at 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.