This is how I understand this, but I'm not familiar with ts , and I'm sure there is a better option.
To get the date from the max / min position, you can index the object created with time on ts . For example: time(data)[which.max(data)] ; for which.min .
Then, to convert this to the corresponding year (simple) and monthly (complex) index, I usually create this function:
numyear2monthyear <- function(x){ c(trunc(x),
Here is an example:
set.seed(123)
And usually I turn this into another convenient function, for example:
extrema_dates <- function(ts){ ts_min_date <- numyear2monthyear(time(ts)[which.min(ts)]) ts_max_date <- numyear2monthyear(time(ts)[which.max(ts)]) list(min=min(ts), min_year=ts_min_date[1], min_month=ts_min_date[2], max=max(ts), max_year=ts_max_date[1], max_month=ts_max_date[2]) } > extrema_dates(data) $min [1] -1.97 $min_year [1] 2013 $min_month [1] 6 $max [1] 2.17 $max_year [1] 2015 $max_month [1] 8
I hope it solves your problem (and would be glad to see the best option for this).
Vincent bonhomme
source share