Marty Panama
Marty Panama

Reputation: 21

xts package: format date as seconds only

I have a matrix X where each column represents a time series. Each row doesn't represent a year, month or day, but rather a second. I'd like to use the xts package but have the dates just be 1,2,3,..., nrow(X), i.e. from 1 to the last second in the series, since each row is one second ahead of the previous. Is this possible? I can't seem to figure it out.

Upvotes: 0

Views: 400

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 270160

1) This can be done with zoo:

library(zoo)
X <- matrix(1:6, 3) + 100 # test data
zoo(X)

giving the following (the 1, 2, 3 column is the times):

1 101 104
2 102 105
3 103 106

2) xts does not support raw numbers as times (see ?xts) but you could use the fact that the "POSIXct" class is expressed in seconds internally. It will show up as POSIXct date times but internally it will be the seconds you asked for:

library(xts)
x <- xts(X, as.POSIXct(1:nrow(X), origin = "1970-01-01"))

giving:

> x
                    [,1] [,2]
1969-12-31 19:00:01  101  104
1969-12-31 19:00:02  102  105
1969-12-31 19:00:03  103  106

> unclass(time(x))
[1] 1 2 3
attr(,"tzone")
[1] ""
attr(,"tclass")
[1] "POSIXct" "POSIXt" 

Upvotes: 1

Related Questions