[R-SIG-Finance] create new columns xts

G See gsee000 at gmail.com
Sun Jun 23 19:25:05 CEST 2013


It might be nice if `[[<-` worked on zoo/xts objects like it does on a
data.frame.
For example,

    X <- as.data.frame(temp)
    n <- 1
    X[[paste0("sma.", n)]] <- SMA(X[[1]])
    tail(X)

But, with zoo/xts objects, temp[[1]] refers to the 1st row of the 1st column.

I _think_ the closest you can get is to pass dimnames= through a new
xts call like this:

    merge(temp, xts(SMA(temp), dimnames=list(NULL, paste0("sma.", n))))

It's a shame that the availability of dimnames= is so hidden (and that it has
to be specified as a list, which is a little weird.)

You probably also wanted your `temp` object to have colnames; otherwise,
temp$sma <- SMA(temp) wouldn't work.  Instead of doing that with
colnames(temp) <- "temp" you could do it when you create the object which can
sometimes be more convenient.

    temp <- xts(1:84, timeCalendar(m=1, d=rep(1:7,each=12),h = seq(0,23,2)),
                dimnames=list(NULL, "temp"))

Garrett

On Sun, Jun 23, 2013 at 11:34 AM, Dominykas Grigonis
<dominykasgrigonis at gmail.com> wrote:
> One more question, that will get me going.
>
> temp <- xts(1:84, timeCalendar(m=1, d=rep(1:7,each=12),h = seq(0,23,2)))
>
> I need to add named column, say SMA(temp)
>
> nice way to do this is temp$sma <- SMA(temp)
>
> however it is inside a function and it might have different SMAs, so I want to adda column named paste0("sma.",n)
>
> temp$paste0("sma.",n) <- SMA(temp)
> temp[,paste0("sma.",n)] <- SMA(temp)
>
> does not work
>
> it is an option to use merge
> merge(temp,SMA(temp))
> and then colnames
>
> However my question is whether it is possible to use temp$paste0("sma.",n) <- SMA(temp) in some way.
> I tried  temp$as.name(paste0("sma.",n)) and similar options...
>
> Thank you.
>
> Kind regards,--
> Dominykas Grigonis
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list