如果我的数据集包含每日数据,我怎样才能group_by一组自定义的日期范围?下面的屏幕截图详细说明了我想要分组的日期范围。任何帮助将不胜感激。谢谢
group_by
2ul0zpep1#
您可以通过创建一个data.frame来实现这一点,在其中您可以定义上面的日期范围,这里我生成了数据来说明这一点:
data.frame
library(dplyr) library(lubridate) # random data dates <- seq(as.Date("2020-01-01"), as.Date("2022-12-31"), by = "day") df <- data.frame(date = dates) # define date ranges ranges <- data.frame( start = ymd(c("2019-06-19", "2020-06-20", "2021-06-21", "2022-06-22")), end = ymd(c("2020-05-20", "2021-05-21", "2022-05-22", "2023-05-23")), group = c("Jun 19 - May 20", "Jun 20 - May 21", "Jun 21 - May 22", "Jun 22 - May 23") ) # breaks for the ranges breaks <- c(ranges$start, ranges$end[nrow(ranges)]) # label for range groups labels <- ranges$group # group data by date ranges df <- df %>% mutate(group = cut(date, breaks = breaks, labels = labels, include.lowest = TRUE)) %>% group_by(group) %>% summarise(mean_value = mean(value))
1条答案
按热度按时间2ul0zpep1#
您可以通过创建一个
data.frame
来实现这一点,在其中您可以定义上面的日期范围,这里我生成了数据来说明这一点: