Saturday, 15 September 2012

r - Get date of timeseries object -



r - Get date of timeseries object -

i have separately created time series object daily frequency:

my.timeseries= ts(data= 1:10, start= c(2014,1,1), frequency = 365.25)

how can dates posixct vector ("2014-01-01 utc" ...) time series object?

here's 1 potential method. i'm not sure if should done way, seems work.

with existing time series, try

p <- paste(attr(my.timeseries, "tsp")[1], my.timeseries) as.posixct(as.date(p, "%y %j")) # [1] "2014-01-01 utc" "2014-01-02 utc" "2014-01-03 utc" # [4] "2014-01-04 utc" "2014-01-05 utc" "2014-01-06 utc" # [7] "2014-01-07 utc" "2014-01-08 utc" "2014-01-09 utc" # [10] "2014-01-10 utc"

as noted g. grothendieck in comments, here more general solution

p <- paste(start(my.timeseries), seq_along(my.timeseries)) as.date(p, "%y %j") # [1] "2014-01-01" "2014-01-02" "2014-01-03" "2014-01-04" # [5] "2014-01-05" "2014-01-06" "2014-01-07" "2014-01-08" # [9] "2014-01-09" "2014-01-10"

as.date might improve avoid time-zone issues.

r date time-series

No comments:

Post a Comment