Skip to content

Indirect references

2 messages · Back2Numbers, Duncan Murdoch

#
Hi All,

I would like to work with symbols referenced by strings: I would like to 
manipulate data/symbols referencing to them by the string name of the 
symbol.

An example will be clearer. Let's I get a time series through quantmod

 > getSymbols("GLD")

This will create a new symbol GLD with the relevant data. I have tried 
to rename the column names as follows:

 > colnames(get("GLD")) <- c("open", "close", "low", "high", "volume", 
"adjusted")

will give the following error:

Error in colnames("GLD")<- c("open", "close", "low", "high", "volume",  :
   target of assignment expands to non-language object


I am confused as to how to do this.

(the intent of this is to maintain a list of tickers in string format 
and loop through them at ease to do whatever treatment)


Thanks for your help,

Emmanuel
#
On 11-11-13 8:10 AM, Back2Numbers wrote:
The syntax

colnames(x) <- y

is a little misleading.  It doesn't really modify the object x, it 
creates a new object then assigns it to x.  You can't assign something 
to "get("GLD")", so you get the error.

The easiest way to do this is not to try to do what quantmod does.  Just 
create new objects and return them from your function.  E.g.

obj <- "GLD"
x <- get(obj)
colnames(x) <- c("open", "close", "low", "high", "volume", "adjusted")

and now x has the names you want.

Duncan Murdoch