Access variable value where the name of variable is stored in a string

Zhang18 picture Zhang18 · Oct 19, 2010 · Viewed 60.2k times · Source

Similar questions have been raised for other languages: C, sql, java, etc.

But I'm trying to do this in R.

I have:

ret_series <- c(1, 2, 3)
x <- "ret_series"

How do I get (1, 2, 3) by calling some function / manipulation on x, without direct mentioning of ret_series?

Answer

Joshua Ulrich picture Joshua Ulrich · Oct 19, 2010

You provided the answer in your question. Try get.

> get(x)
[1] 1 2 3