开发者

Finding the date of a data point in a R time-series

I make a time series:

开发者_如何学编程

t = ts(rnorm(12*50), start=1900, freq=12)

and then find the maximum with max(t). Is there any convenient way to find out what date this maximum occurred at?


Try which.max() which works for many object types, including ts as in your example:

R> set.seed(42); tser <- ts(rnorm(12*2), start=2010, freq=12)
R> which.max(tser)
[1] 12                              ## so index 12 is suggsted
R> tser[12]                         ## what is its value?       
[1] 2.28665                         ## 2.28665 -- indeed the max.
R> tser                             
            Jan        Feb        Mar        Apr        May
2010  1.3709584 -0.5646982  0.3631284  0.6328626  0.4042683
2011 -1.3888607 -0.2787888 -0.1333213  0.6359504 -0.2842529
            Jun        Jul        Aug        Sep        Oct
2010 -0.1061245  1.5115220 -0.0946590  2.0184237 -0.0627141
2011 -2.6564554 -2.4404669  1.3201133 -0.3066386 -1.7813084
            Nov        Dec
2010  1.3048697  2.2866454
2011 -0.1719174  1.2146747
R> 

If you convert from ts to, say, zoo you even get the meta-data displayed:

R> zser <- as.zoo(tser)
R> which.max(zser)
[1] 12
R> zser[12]
2010(12) 
 2.28665 
R> 

That shows the Dec 2010 label for the data point with the maximum value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜