R: Why an xts object becomes a zoo object after calling transform()?
Question
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?
Solution
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
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow