[R] [External] Convert a character string to variable names

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Tue Feb 8 00:05:41 CET 2022


> x <- c("mtcars$disp", "mtcars$hp", "mtcars$cyl")
> x
[1] "mtcars$disp" "mtcars$hp"   "mtcars$cyl" 
> eval(parse(text=x))
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
> for (i in x) print(eval(parse(text=i)))
 [1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8 275.8 275.8 472.0 460.0 440.0  78.7  75.7  71.1 120.1 318.0
[23] 304.0 350.0 400.0  79.0 120.3  95.1 351.0 145.0 301.0 121.0
 [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52  65  97 150 150 245 175  66  91 113 264 175 335 109
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4


> On Feb 07, 2022, at 17:55, Erin Hodgess <erinm.hodgess using gmail.com> wrote:
> 
>> .x
> 
> [1] "mtcars$disp" "mtcars$hp"   "mtcars$cyl"



More information about the R-help mailing list