[R] Indirect references
Duncan Murdoch
murdoch.duncan at gmail.com
Sun Nov 13 15:52:05 CET 2011
On 11-11-13 8:10 AM, Back2Numbers wrote:
> 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 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
More information about the R-help
mailing list