简体   繁体   English

R Select 多年的日期范围并计算平均值

[英]R Select date range over multiple years and calculate mean of values

I have a data frame with hourly data running over 5 years.我有一个数据框,每小时数据运行超过 5 年。 I want to calculate the hourly mean (ie, the mean value for every hour of the day, 1:24) of values between two dates (eg, 15-March to 15-Apr) over several years, and compare that to the hourly mean of the last year.我想计算几年内两个日期(例如,3 月 15 日至 4 月 15 日)之间的值的每小时平均值(即一天中每个小时的平均值,1:24),并将其与每小时进行比较去年的平均值。

Here is an example of the data:以下是数据示例:

start = as.POSIXct(strptime("2011-01-01 01:00", "%Y-%m-%d %H:%M"))
end   = as.POSIXct(strptime("2016-01-01 01:00", "%Y-%m-%d %H:%M"))
df = data.frame(DateTime = seq(from = start, to = end,by = "hours"))
df$value = runif(nrow(df))

Start_Period = "03-15"
End_Period = "04-15"

The output should look like: output 应如下所示:

Hour   mean(2011-2014) mean(2015)
1      0.3             0.5
...
24     0.8             0.6

We can filter based on the 'start', 'end' date, then do a group by 'hour' 'year' and get the mean我们可以根据“开始”、“结束”日期进行filter ,然后按“小时”“年”进行分组并获得mean

library(lubridate)
library(dplyr)   
df %>%
    filter((day(DateTime) >= 15 & month(DateTime) == 3)|
          (day(DateTime) <= 15 & month(DateTime) ==  4))   %>% 
    group_by(hour = hour(DateTime), year = year(DateTime)) %>% 
    summarise(value = mean(value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM