[R-SIG-Finance] timeseries - xst vs. dataframe?

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Feb 13 15:43:39 CET 2008


On Wed, 13 Feb 2008, Gabor Grothendieck 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.

True, and this is the explanation why we don't have it at the moment. But
the $ operator might be a convenient addition. And we currently have a few
examples where we are consistent with "ts" but provide further features
for convenience. At the moment, I don't think something dangerous would
happen if we add it - or do I overlook something?

Given that you have already written the code, I would vote for including
it in the package.
Z

> 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
>
> > dataframes.  Is "list" syntax planned for inclusion in xst? At
> > present, column numbering (test.zoo[,1]) seems the best alternative.
> >
> > Since as.data.frame(test.zoo)$a appears to recover the core data, it
> > sounds sensible for "test.zoo$a" to extract an object containing the
> > index and the named column.  Does this break anything?
> > e.g. :
> >
> > test.df = data.frame(a=1:5, b=2*(1:5))
> > test.df$a
> > #[1] 1 2 3 4 5
> >
> > index = Sys.time() + 60*1:5
> > test.zoo = zoo(test.df, order.by=index)
> > test.zoo$a
> > #NULL
> >
> > test.zoo[1:2,1]
> > # 2008-02-13 05:54:40 2008-02-13 05:55:40
> > #                  1                   2
> >
> > coredata(test.zoo)$a
> > #NULL
> >
> > as.data.frame(test.zoo)$a
> > # [1] 1 2 3 4 5
> >
> > thanks and best,
> > christian
> >
> > > At this point 'xts' objects behave much like any standard data.frame, matrix or, most closely, zoo object.  They have some unique user 'xts' methods but all standard 'zoo' methods will work (it just extends 'zoo')
> >
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>



More information about the R-SIG-Finance mailing list