每日最大时间

时间:2016-01-12 08:57:51

标签: r time

我有温度和其他参数的时间序列数据集 我正在使用apply.daily函数来提取每日最高温度

DailyMaxAirTemp <- apply.daily(xx_select1$Temp_C, max,na.rm=TRUE)

xx_select是一个.xts对象

                    Temp_C   
2015-07-01 00:00:00 " 11.977"
2015-07-01 00:10:00 " 11.880"   
2015-07-01 00:20:00 " 11.589"

DailyMaxAirTemp返回:

DailyMaxAirTemp
                    Temp_C   
2015-07-01 23:55:00 " 26.598"
2015-07-02 23:55:00 " 27.063"
2015-07-03 23:55:00 " 29.015"
2015-07-04 23:55:00 " 30.091"
2015-07-05 23:55:00 " 31.996"
2015-07-06 23:55:00 " 29.115"
....

但我也希望得到与最大值相关的时间,这样我才能知道峰值温度出现的平均时间,而不是&#34; 23:55:00&#34;每一天 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

可以直接获得与每个最高温度相关的时间。使用您在上面定义的xts对象,

# max temp for each day
Tmax <- apply.daily(xx_select1$Temp_C[,1], max, na.rm=TRUE)
# time of day of max temp
timeTmax <- index(xx_select1$Temp_C)[xx_select1$Temp_C[,1] %in% Tmax]

我认为解决平均时间问题的最佳方法是通过lubridate包。

library(lubridate)
# make all dates the same
times <- update(timeTmax, year=2000, month=1, mday=1)
# mean time of day for max temp
format(mean(times), format="%H:%M:%S")