Devang Akotia - 1 year ago 109
R Question

# how to extract timestamps from ts object in r

Consider the treerings dataset.

``````library("datasets", lib.loc="C:/Program Files/R/R-3.3.1/library")
tr<-treering

length(tr)
[1] 7980

class(tr)
[1] "ts"
``````

From my understanding, it is a time series of length 7980.
How can I find out what the time stamps are for each value?

After plotting the time series, looking at the x axis of the plot, it appears that the time stamps range between -6000 to 2000. But to me the time stamps appear to be "hidden".

``````plot(tr)
``````

More generally, I'm trying to understand what exactly is a
`ts`
object and what are the benefits of using this type of object.

A univariate and multivariate time series can easily be displayed in a data frame with 2 or more columns: Time and variables .

``````univariatetimeseries <- data.frame(Time = c(0, 1, 2, 3, 4, 5, 6), y = c(1, 2, 3, 4, 5, 6, 7))
multivariatetimeseries <- data.frame(Time = c(0,1,2,3,4,5,6), y = c(1, 2, 3, 4, 5, 6, 7), z = c(7,6,5,4,3,2,1))
``````

This to me seems simple and straighforward and it is consistent with the basic science examples that I learned in high school. Additionally, the time stamps are not "hidden" as is the case of the
`treering`
example. So what are the benefits of using
`ts`
object?

Object of class comes with many generic functions for convenience. Say for "ts" object class there are `ts.plot`, `plot.ts`, etc. If you store your time series as a data frame, you have to do lots of work yourself when plotting them.
Perhaps for seasonal time series, the advantage of using "ts" is more evident. For example, `x <- ts(rnorm(36), start = c(2000, 1), frequency = 12)` generates monthly time series for 3 years. The `print` method will nicely arrange it like a matrix when you print `x`.
A "ts" object has a number of attributes. Modelling fitting routines like `arima0` and `arima` can see such attributes so you don't need to specify them manually.
For your question, there are a number of functions to extract / set attributes of a time series. Have a look at `?start`, `?tsp`, `?time`, `?window`.