[R] Changing Attribute With Paste

Duncan Murdoch murdoch.duncan at gmail.com
Sat May 14 12:42:35 CEST 2011


On 11-05-14 4:14 AM, Sparks, John James wrote:
> Dear R Helpers,
>
> I am trying to adjust the attribute of an R object pulled from quantmod.
> Since I want to do this for many such objects, I was trying to make the
> adjustment programmatic.  Unfortunately, I am having a huge amount of
> trouble using attr in combination with paste (and perhaps get, and perhaps
> assign, none of which seem to help).  When I hard-code the change it works
> fine.  Your help would be much appreciated.
>
>
> require(quantmod)
> getFin("NYSE:A")
>
> attr(NYSE.A.f,"symbol")<-"A"  #works fine
>
> ticker<-"A"
> attr(paste("NYSE.",ticker,".f",sep=""),"symbol")<-"A"         #doesn't work
> attr(get(paste("NYSE.",ticker,".f",sep="")),"symbol")<-"A"    #nor does
> this, nor the hundred other combinations I have tried

This should work:

name <- paste("NYSE.",ticker,".f",sep="")
x <- get(name)
attr(x, "symbol") <- "A"
assign(name, x)

Duncan Murdoch



More information about the R-help mailing list