[R] Use a text variable's value to specify another varaible?
Marc Schwartz
marc_schwartz at comcast.net
Fri Jan 26 17:00:49 CET 2007
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 Fairbank
See ?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
More information about the R-help
mailing list