Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Time Series Object ts() Date of Minimum and Maximum

Tags:

r

time-series

I would like to find the date of the minimum and maximum values in an R ts() object. I tried the which.min and which.max functions but they only return the "row number." I would like to output the actual Date. Thank you.

data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
data
which.min(data)
which.max(data)
like image 812
user1491868 Avatar asked Oct 01 '17 18:10

user1491868


1 Answers

My favourite tool for time series is xts, and ts objects translate cleanly:

library(xts)
x = as.xts(data)

Outputs:

> min(index(x))
[1] "Jan 2012"
> max(index(x))
[1] "Dec 2016"
like image 97
lebelinoz Avatar answered Oct 01 '22 21:10

lebelinoz