8

I want to know the differences into use ts() or zoo() function.

xusliebana
  • 303
  • 1
  • 4
  • 11
  • 2
    One creates a `"ts"` class object and the other creates a `"zoo"` class object. Try `?ts` and the documentation that comes with the zoo package (help files, 5 vignettes). ts objects are regularly spaced and have numeric times and are good for months and quarters whereas zoo objects can be irregularly spaced and can use most common index classes. – G. Grothendieck Nov 14 '15 at 23:42
  • `ts()` is a base R function for time series. `zoo` is a separate package. I strongly suggest you go with the more complete `zoo`. If you ever need to transform your `zoo` objects into `ts`, you can always use `xts` to do so. – Pierre Lapointe Nov 14 '15 at 23:44
  • 2
    xts is not used to convert between ts and zoo. One would use as.ts and as.zoo and these would dispatch to as.ts.zoo and as.zoo.ts from the zoo package. – G. Grothendieck Nov 15 '15 at 00:13
  • 3
    I never understood why people use the comments section to (try to) answer a question, @G.Grothendieck – agoldev Sep 07 '17 at 12:56
  • @agoldev Perhaps he thought that pointing the questioner to the documentation was sufficient. After all, he probably wrote much of it, at least for the zoo-package. – IRTFM Sep 08 '17 at 15:54

1 Answers1

12

A zoo object has the time values (possibly irregular) in an index attribute displayed like a row name at the console by the print.zoo method and the values in a matrix or atomic vector which places constraints on the values that can be used (generally numeric, but necessarily all of a single mode, i.e. not as a list with multiple modes like a dataframe might hold). With pkg:zoo loaded, to get a list of functions that have zoo-methods:

library(zoo)
methods(class="zoo")

The yrmon- class is added to allow monthly date indices. you can see the range of methods:

methods(class="yearmon")

The xts-class is an important extension to the zoo methods but an additional package is needed. There are many worked examples of zoo and xts functions on SO.

A ts-object has values of a single mode with attributes that always imply regular observations and those attributes support a recurring cycle such as years and months. Rather than storing the index item by item or row by row, the index is calculated on the fly using 'start', 'end' and 'frequency' values stored as attributes and accessible with functions by those names. The list of functions for ts-objects is distinctly small (and most people find them more difficult to work with):

methods(class="ts")

There was also an its-package for irregular time series, but it was distinctly less popular than the zoo-package and has apparently been abandoned.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 2
    This answer is nearly two years old but here are a few up to date corrections anyways: ts and zoo objects don't have to be numeric, e.g. `ts(letters); z <- zoo(letters)`, zoo objects can be printed vertically or horizontally, e.g. `print(z, style = "v"); print(z, style = "h")` and the data part of a zoo object can be a vector or matrix, not just a matrix, e.g. `zoo(anscombe); read.zoo(BOD)` The its package was removed from CRAN last December (Dec 2016). – G. Grothendieck Sep 07 '17 at 13:37
  • Thanks for the correction. I hope my answer is now more correct. Other readers should be aware that G.Grothendieck is one of the authors of the zoo-package and has answered many zoo-related questions on rhelp and SO. Anything he writes on such matters can be considered canonical. – IRTFM Sep 08 '17 at 15:48