tags:

views:

67

answers:

3

similar questions have raised for other languages: C, sql, java, etc. But I'm trying to do it 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?

+6  A: 

You provided the answer in your question. Try get.

> get(x)
[1] 1 2 3
Joshua Ulrich
+3  A: 

For a one off use, the get function works (as has been mentioned), but it does not scale well to larger projects. it is better to store you data in lists or environments, then use [[ to access the individual elements:

mydata <- list( ret_series=c(1,2,3) )
x <- 'ret_series'

mydata[[x]]
Greg Snow
A: 

What's wrong with either of the following?

eval(as.name(x))

eval(as.symbol(x))
RomanM
Be careful when pasting code from console. StackOverflow interprets ">" as citing. Use 0101010 symbol to format code. And welcome on SO ;)
Marek