0

I am very new to R. I am looking to write a query, for example

hello_world<- c(1,2,3,4)
hi_world <- c(5,6,7)

x<- c(paste("hello","world",sep="_"),paste("hi","world",sep="_"))

I have stored the previously defined variable names as string inside the variable x

Now from x, how can I call out the any of the 2 variables so that it displays

5 6 7

If I call x[2] which is actually hi_world?

halfer
  • 19,824
  • 17
  • 99
  • 186
Deb
  • 295
  • 1
  • 3
  • 11
  • 1
    Ok, I got it from an older post http://stackoverflow.com/questions/3971844/access-variable-value-where-the-name-of-variable-is-stored-in-a-string-in-r – Deb Aug 08 '16 at 15:27
  • 1
    I'm glad you solved your problem, but if you're just getting started with R, please be aware that relying on `get` is a bad habit, particularly for inexperienced R programmers. It will often lead to code patterns that are quite problematic. – joran Aug 08 '16 at 15:30
  • Joran - is there a more cleaner way to do this? If you can please guide – Deb Aug 08 '16 at 15:31
  • 1
    A more standard pattern would be to put your vectors in a named list: `my_list <- list(hello_world = 1:4,hi_world = 5:7)`. Then you can do `my_list[[x[1]]]`. The main problem with `get` is that eventually you're going to want to use it to retrieve an object _and_ assign a new value to it, and that generally won't work. – joran Aug 08 '16 at 15:33

0 Answers0