Re: [R] Use a text variable's value to specify another varaible?

From: Marc Schwartz <>
Date: Fri 26 Jan 2007 - 16:00:49 GMT

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


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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Sat Jan 27 03:04:42 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 26 Jan 2007 - 16:30:30 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.