Search code examples
rinverse

How do I convert ts based on yday() back to proper dates


I have a periodic ts with frequency of 365 in R using yday to use in forecasting. How do I convert the ts back to a day of the year ?

myTs = ts(rnorm(1:1000), start=c(year(as.Date("2018-05-05")),yday("2018-05-05")),frequency = 365)
head(index(myTs))

I get the following:

[1] 2018.340 2018.342 2018.345 2018.348 2018.351 2018.353

I use this ts for a forecast. But I want to be able to convert back the index of the forecast output to the actual day of the year in my post processing, and I could not find such a function in the documentation. I want to avoid writing such a function if possible.

Is there a function that converts the elements of the ts index back to the yday, i.e. 2018.340 back to 125.


Solution

  • Clemsang's answer:

    > format(date_decimal(2016.425),"%Y-%m-%d")
    [1] "2016-06-04"