Ben Fairbank
2007-Jan-26 15:48 UTC
[R] Use a text variable's value to specify another varaible?
Greetings guRus -- If a variable, e.g., 'varname', is a character string, e.g. varname <- "datavector", and I want to apply a function, such as table(), to datavector, what syntax or method will do so using only the variable varname? This seems similar to indirect addressing, but I have not seen a method for it in the R manuals. Is there a general name for such indirect reference that one might search for? (This came up while writing a function that takes the value of 'varname' from the keyboard and then applies functions to it.) With thanks for any solution, Ben Fairbank [[alternative HTML version deleted]]
Marc Schwartz
2007-Jan-26 16:00 UTC
[R] Use a text variable's value to specify another varaible?
On Fri, 2007-01-26 at 09:48 -0600, Ben Fairbank wrote:> Greetings guRus --> If a variable, e.g., 'varname', is a character string, e.g. varname <- > "datavector", and I want to apply a function, such as table(), to > datavector, what syntax or method will do so using only the variable > varname? This seems similar to indirect addressing, but I have not seen > a method for it in the R manuals. Is there a general name for such > indirect reference that one might search for?> (This came up while writing a function that takes the value of 'varname' > from the keyboard and then applies functions to it.)> With thanks for any solution,> Ben FairbankSee ?get and ?assign datavector <- 1:10 varname <- "datavector"> get(varname)[1] 1 2 3 4 5 6 7 8 9 10> log10(get(varname))[1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700 0.7781513 [7] 0.8450980 0.9030900 0.9542425 1.0000000 and/or assign(varname, letters)> datavector[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"> get(varname)[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" [18] "r" "s" "t" "u" "v" "w" "x" "y" "z" HTH, Marc Schwartz
Erik Iverson
2007-Jan-26 16:19 UTC
[R] Use a text variable's value to specify another varaible?
Can I ask why you aren't just passing in the object to your function, but instead a text name for that object? Ben Fairbank wrote:> Greetings guRus -- > > > > If a variable, e.g., 'varname', is a character string, e.g. varname <- > "datavector", and I want to apply a function, such as table(), to > datavector, what syntax or method will do so using only the variable > varname? This seems similar to indirect addressing, but I have not seen > a method for it in the R manuals. Is there a general name for such > indirect reference that one might search for? > > > > (This came up while writing a function that takes the value of 'varname' > from the keyboard and then applies functions to it.) > > > > With thanks for any solution, > > > > Ben Fairbank > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.