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.
我想在R ts()对象中找到最小值和最大值的日期。我尝试了which.min和which.max函数,但它们只返回“行号”。我想输出实际的日期。谢谢。
data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
data
which.min(data)
which.max(data)
4 个解决方案
#1
4
This is how you find the year and month of the min and max value in the data:
这是您在数据中找到最小值和最大值的年份和月份的方法:
> data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
> data
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2012 0.18 -0.07 -0.77 1.23 -0.97 1.20 -1.41 1.39 -0.72 -0.94 0.28 0.97
2013 -0.86 -0.57 -0.16 -1.24 -0.35 -0.06 0.78 1.32 1.80 -0.51 -1.91 1.14
2014 -0.51 1.21 0.14 0.30 1.18 -0.32 -0.92 -0.46 -0.97 -0.94 -1.56 -0.63
2015 0.13 0.93 -1.45 1.97 0.04 0.55 0.45 0.13 1.14 0.27 0.15 -1.39
2016 0.68 2.16 -1.56 -0.44 1.07 1.27 1.01 -2.93 -0.19 -0.70 1.44 0.09
The corresponding date of the minimum date is
最短日期的相应日期是
> data_min_value <- data[which.min(data)]
> data_min_value
[1] -2.93
> data_min_value_time <- time(data)[which.min(data)]
> data_min_value_time
[1] 2016.583
> data_min_value_year <- floor(time(data)[which.min(data)])
> data_min_value_year
[1] 2016
> data_min_value_month <- (time(data)[which.min(data)] %% 1)*12
> data_min_value_month
[1] 7
> data_min_value_month_abb <- month.abb[(time(data)[which.min(data)] %% 1)*12+1]
> data_min_value_month_abb
[1] "Aug"
The date of the maximum value you get similarly
您获得的最大值的日期类似
> data[which.max(data)]
[1] 2.16
> floor(time(data)[which.max(data)]) # Year
[1] 2016
> month.abb[(time(data)[which.max(data)] %% 1)*12+1] # Abbreviation of month
[1] "Feb"
Below is summary on the helpful functions which have been used in the above examples:
以下是上述示例中使用的有用功能的摘要:
> floor(2016.563) # find out the integer part on the number
[1] 2016
> 2016.563 %% 1 # find out the fractional part on the number
[1] 0.563
> month.abb[0.563*12+1] # find out the abbreviation of the month name
[1] "Jul"
#2
5
Here is how I deal with that but I'm not familiar with ts
and I'm sure there is a better option.
这是我如何处理,但我不熟悉ts,我相信有更好的选择。
To retrieve the date from the max/min position, you can index the object created by time
on your ts
. Eg: time(data)[which.max(data)]
; same for which.min
.
要从最大/最小位置检索日期,您可以索引在ts上按时间创建的对象。例如:时间(数据)[which.max(data)];同样的.min。
Then to convert this into a proper year (easy) and month (tricky) index, I usually create this function:
然后将其转换为适当的年份(简单)和月份(棘手)索引,我通常会创建此函数:
numyear2monthyear <- function(x){
c(trunc(x), # entire part = year
round((x-floor(x))*12 + 1)) # decimal part * 12 + 1 (Jan=0) = Month
}
Here is an example:
这是一个例子:
set.seed(123) # for the sake of reproducibility
data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
data
Jan Feb Mar Apr May Jun Jul Aug
2012 -0.56 -0.23 1.56 0.07 0.13 1.72 0.46 -1.27
2013 0.40 0.11 -0.56 1.79 0.50 -1.97 0.70 -0.47
2014 -0.63 -1.69 0.84 0.15 -1.14 1.25 0.43 -0.30
2015 0.55 -0.06 -0.31 -0.38 -0.69 -0.21 -1.27 2.17
2016 0.78 -0.08 0.25 -0.03 -0.04 1.37 -0.23 1.52
Sep Oct Nov Dec
2012 -0.69 -0.45 1.22 0.36
2013 -1.07 -0.22 -1.03 -0.73
2014 0.90 0.88 0.82 0.69
2015 1.21 -1.12 -0.40 -0.47
2016 -1.55 0.58 0.12 0.22
which.min(data)
[1] 18
which.max(data)
[1] 44
numyear2monthyear(time(data)[which.max(data)])
[1] 2015 8
numyear2monthyear(time(data)[which.min(data)])
[1] 2013 6
And usually I turn that into another handy function, like:
通常我把它变成另一个方便的功能,如:
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 happy to see a better option to do it).
我希望它能解决你的问题(并且很乐意看到更好的选择)。
#3
3
This gives a yearmon class object for the minimum. The same with which.max
would work for the maximum.
这给了一个最小的yearmon类对象。与max.max相同的最大值。
library(zoo)
imin <- which.min(data)
tmin <- time(as.zoo(data))[imin]
tmin
## [1] "Dec 2016"
as.integer(tmin)
, cycle(tmin)
and as.Date(tmin)
would give the year, month (as number between 1 and 12) and a Date class object using the 1st of the month respectively.
as.integer(tmin),cycle(tmin)和as.Date(tmin)分别使用月份的第1个给出年,月(作为1到12之间的数字)和Date类对象。
Alternately, time(data)[imin]
, as.integer(time(data))[imin]
and cycle(data)[imin]
will give the time as year + fraction, year as integer and month number. These three do not use any packages.
或者,时间(数据)[imin],as.integer(时间(数据))[imin]和cycle(data)[imin]将给出时间作为年份+分数,年份作为整数和月份数量。这三个不使用任何包。
#4
3
My favourite tool for time series is xts
, and ts
objects translate cleanly:
我最喜欢的时间序列工具是xts,ts对象可以干净地翻译:
library(xts)
x = as.xts(data)
Outputs:
> min(index(x))
[1] "Jan 2012"
> max(index(x))
[1] "Dec 2016"
#1
4
This is how you find the year and month of the min and max value in the data:
这是您在数据中找到最小值和最大值的年份和月份的方法:
> data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
> data
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2012 0.18 -0.07 -0.77 1.23 -0.97 1.20 -1.41 1.39 -0.72 -0.94 0.28 0.97
2013 -0.86 -0.57 -0.16 -1.24 -0.35 -0.06 0.78 1.32 1.80 -0.51 -1.91 1.14
2014 -0.51 1.21 0.14 0.30 1.18 -0.32 -0.92 -0.46 -0.97 -0.94 -1.56 -0.63
2015 0.13 0.93 -1.45 1.97 0.04 0.55 0.45 0.13 1.14 0.27 0.15 -1.39
2016 0.68 2.16 -1.56 -0.44 1.07 1.27 1.01 -2.93 -0.19 -0.70 1.44 0.09
The corresponding date of the minimum date is
最短日期的相应日期是
> data_min_value <- data[which.min(data)]
> data_min_value
[1] -2.93
> data_min_value_time <- time(data)[which.min(data)]
> data_min_value_time
[1] 2016.583
> data_min_value_year <- floor(time(data)[which.min(data)])
> data_min_value_year
[1] 2016
> data_min_value_month <- (time(data)[which.min(data)] %% 1)*12
> data_min_value_month
[1] 7
> data_min_value_month_abb <- month.abb[(time(data)[which.min(data)] %% 1)*12+1]
> data_min_value_month_abb
[1] "Aug"
The date of the maximum value you get similarly
您获得的最大值的日期类似
> data[which.max(data)]
[1] 2.16
> floor(time(data)[which.max(data)]) # Year
[1] 2016
> month.abb[(time(data)[which.max(data)] %% 1)*12+1] # Abbreviation of month
[1] "Feb"
Below is summary on the helpful functions which have been used in the above examples:
以下是上述示例中使用的有用功能的摘要:
> floor(2016.563) # find out the integer part on the number
[1] 2016
> 2016.563 %% 1 # find out the fractional part on the number
[1] 0.563
> month.abb[0.563*12+1] # find out the abbreviation of the month name
[1] "Jul"
#2
5
Here is how I deal with that but I'm not familiar with ts
and I'm sure there is a better option.
这是我如何处理,但我不熟悉ts,我相信有更好的选择。
To retrieve the date from the max/min position, you can index the object created by time
on your ts
. Eg: time(data)[which.max(data)]
; same for which.min
.
要从最大/最小位置检索日期,您可以索引在ts上按时间创建的对象。例如:时间(数据)[which.max(data)];同样的.min。
Then to convert this into a proper year (easy) and month (tricky) index, I usually create this function:
然后将其转换为适当的年份(简单)和月份(棘手)索引,我通常会创建此函数:
numyear2monthyear <- function(x){
c(trunc(x), # entire part = year
round((x-floor(x))*12 + 1)) # decimal part * 12 + 1 (Jan=0) = Month
}
Here is an example:
这是一个例子:
set.seed(123) # for the sake of reproducibility
data <- ts(round(rnorm(60), 2), frequency = 12, end = c(2016, 12))
data
Jan Feb Mar Apr May Jun Jul Aug
2012 -0.56 -0.23 1.56 0.07 0.13 1.72 0.46 -1.27
2013 0.40 0.11 -0.56 1.79 0.50 -1.97 0.70 -0.47
2014 -0.63 -1.69 0.84 0.15 -1.14 1.25 0.43 -0.30
2015 0.55 -0.06 -0.31 -0.38 -0.69 -0.21 -1.27 2.17
2016 0.78 -0.08 0.25 -0.03 -0.04 1.37 -0.23 1.52
Sep Oct Nov Dec
2012 -0.69 -0.45 1.22 0.36
2013 -1.07 -0.22 -1.03 -0.73
2014 0.90 0.88 0.82 0.69
2015 1.21 -1.12 -0.40 -0.47
2016 -1.55 0.58 0.12 0.22
which.min(data)
[1] 18
which.max(data)
[1] 44
numyear2monthyear(time(data)[which.max(data)])
[1] 2015 8
numyear2monthyear(time(data)[which.min(data)])
[1] 2013 6
And usually I turn that into another handy function, like:
通常我把它变成另一个方便的功能,如:
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 happy to see a better option to do it).
我希望它能解决你的问题(并且很乐意看到更好的选择)。
#3
3
This gives a yearmon class object for the minimum. The same with which.max
would work for the maximum.
这给了一个最小的yearmon类对象。与max.max相同的最大值。
library(zoo)
imin <- which.min(data)
tmin <- time(as.zoo(data))[imin]
tmin
## [1] "Dec 2016"
as.integer(tmin)
, cycle(tmin)
and as.Date(tmin)
would give the year, month (as number between 1 and 12) and a Date class object using the 1st of the month respectively.
as.integer(tmin),cycle(tmin)和as.Date(tmin)分别使用月份的第1个给出年,月(作为1到12之间的数字)和Date类对象。
Alternately, time(data)[imin]
, as.integer(time(data))[imin]
and cycle(data)[imin]
will give the time as year + fraction, year as integer and month number. These three do not use any packages.
或者,时间(数据)[imin],as.integer(时间(数据))[imin]和cycle(data)[imin]将给出时间作为年份+分数,年份作为整数和月份数量。这三个不使用任何包。
#4
3
My favourite tool for time series is xts
, and ts
objects translate cleanly:
我最喜欢的时间序列工具是xts,ts对象可以干净地翻译:
library(xts)
x = as.xts(data)
Outputs:
> min(index(x))
[1] "Jan 2012"
> max(index(x))
[1] "Dec 2016"