Julian Wergieluk
Julian Wergieluk

Reputation: 135

ts class - converting time stamps to numbers

I encountered a somehow strange behavior of the ts class regarding the storage mechanisms of the start and end time stamps. Example:

> K <- ts(c(1:10), start=0., end=1., deltat=0.1)
> start(K); end(K)
[1] 0 1
[1] 1 1

I get the start and end dates as natural time units, as described in ?ts. Sometimes I get the time stamps as single numbers:

> K <- ts(c(1:10), start=0.123, end=1.123, deltat=0.1)
> start(K); end(K)
[1] 0.123
[1] 1.123

Is there a way to force ts to store the dates as numbers and not as 2-vectors? Or, alternatively, is there a reliable method to convert the 2-vector representation to a number. I want to be able to retrive t0 and t1 out of K defined as

K <- ts(c(1:n), start=t0, end=t1, deltat=dt)

Upvotes: 1

Views: 145

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270170

tsp. It does not store anything as 2-vectors -- it only represents it that way as the values of start and end. tsp(K)[1] and tsp(K)[2] will give the start and end in the way you want. See ?tsp .

zoo. Another approach would be:

library(zoo)
start(as.zoo(K))
end(as.zoo(K))

Upvotes: 1

Related Questions