0

I have an object that contains the name of a variable as a string example :

OBJECT_containing_variables_name <- "the_variable_I_want_to_read"

Is there a function I can use to read the_variable_I_want_to_read using the OBJECT_containing_variables_name as a string?



In fact, I am trying to make a for loop, and I construct the name of my variable with

paste0("tablename$",eval(parse(text= "colnames_as_string[i]")))

This actually retuns the name of my variable as a string like "tablename$colname"

But I want to actually read the variable itself. How can I do this ?

I made some tests :

  • I already tried eval and parse functions like :

    eval(  paste0("tablename$",eval(parse(text="colnames_as_string[i]"))) )
    

but it also returns the name of the variable as a string

  • I also tried :

    eval(parse(text=" paste0("tablename$",eval(parse(text="colnames_as_string[i]")))  "))
    

but it still returns the name of the variable as a string.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names may be useful – user20650 Sep 24 '19 at 10:04
  • `eval(parse(text =paste0("tablename$",eval(parse(text= "colnames_as_string[i]")))))` should work. See also https://stackoverflow.com/questions/9057006/getting-strings-recognized-as-variable-names-in-r – Chelmy88 Sep 24 '19 at 10:11
  • This is atrocious code. If you get into the habit of writing code with `eval(parse())` you will regret that sooner or later. My guess is that you should simply do `tablename[[colnames_as_string[i]]]`. – Roland Sep 24 '19 at 10:16
  • @Roland Your code works perfectly for me !! And yes, it is much easier. Thank you very much – Elodie Hochscheid Sep 24 '19 at 10:23
  • Add the solution as an answer and mark it correct instead of adding '#solved' to the title – Beavis Sep 24 '19 at 11:03

1 Answers1

0

Too long to paste as a comment, so an answer instead. If you're using data.frame and want to retrieve a column given the column name is stored as vector (k, say), here's what you can do:

> w <- as.data.frame(mtcars)
> k <- colnames(w)[1:5]
> k
[1] "mpg"  "cyl"  "disp" "hp"   "drat"
> w[, k[1]]
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7
[18] 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4
> w$mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7
[18] 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4

In a loop this could be achieved using the apply family of functions:

# Testing just one column 
> paste(w[, k[1]], collapse = " ")
[1] "21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26 30.4 15.8 19.7 15 21.4"

# Using sapply to get a vector with all column names in 'k'
> sapply(k, function(z) paste(w[, z], collapse = " "))
                                                                                                                                                        mpg 
  "21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26 30.4 15.8 19.7 15 21.4" 
                                                                                                                                                        cyl 
                                                                                          "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" 
                                                                                                                                                       disp 
     "160 160 108 258 360 225 360 146.7 140.8 167.6 167.6 275.8 275.8 275.8 472 460 440 78.7 75.7 71.1 120.1 318 304 350 400 79 120.3 95.1 351 145 301 121" 
                                                                                                                                                         hp 
                                   "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" 
                                                                                                                                                       drat 
"3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07 2.93 3 3.23 4.08 4.93 4.22 3.7 2.76 3.15 3.73 3.08 4.08 4.43 3.77 4.22 3.62 3.54 4.11" 
Gautam
  • 2,597
  • 1
  • 28
  • 51