R: Why an xts object becomes a zoo object after calling transform()?

怎甘沉沦 提交于 2019-12-10 11:32:11

问题


transform() removes the class "xts" qualifier from my xts object:

> class(myxts)
[1] "xts" "zoo"
> myxts = transform(myxts, ABC = 1)
> class(myxts)
[1] "zoo"

Why is that?


回答1:


There's no xts method for transform, so the zoo method is dispatched. The zoo method explicitly creates a new zoo object.

> zoo:::transform.zoo
function (`_data`, ...) 
{
    if (is.null(dim(coredata(`_data`)))) 
        warning("transform() is only useful for matrix-based zoo series")
    zoo(transform.data.frame(data.frame(coredata(`_data`)), ...), 
        index(`_data`), attr(`_data`, "frequency"))
}
<environment: namespace:zoo>

You could simply wrap your transform calls in as.xts, or your example could be written as myxts$ABC <- 1.



来源:https://stackoverflow.com/questions/9332045/r-why-an-xts-object-becomes-a-zoo-object-after-calling-transform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!