Skip to content
Prev 2165 / 15274 Next

timeseries - xst vs. dataframe?

On Feb 13, 2008 8:17 AM, icosa atropa <icos.atropa at gmail.com> wrote:
zoo is modelled on the "ts" class, not on the "data.frame" class.
In R, the way it works is that $ is used on list-based objects
and not on array-based objects.  Of course, your are free to define
and redefine operators as you please and since zoo is an S3 class
its possible to add your own S3 methods.   $ indexing is less
than a dozen lines of code to add to your program:

"$.zoo" <- function(object, x) object[, x]

"$<-.zoo" <- function(object, x, value) {
    stopifnot(length(dim(object)) == 2)
    if (x %in% colnames(object)) object[,x] <- value
    else {
        object <- cbind(object, value)
        colnames(object)[ncol(object)] <- x
    }
    object
}

# test
library(zoo)
z <- zoo(cbind(a = 1:3, b = 4:6))
z$c <- z$b + 1
z$a <- z$b - 1
z