按R中的不同日期范围分组?

ujv3wf0j  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(130)

如果我的数据集包含每日数据,我怎样才能group_by一组自定义的日期范围?下面的屏幕截图详细说明了我想要分组的日期范围。任何帮助将不胜感激。
谢谢

2ul0zpep

2ul0zpep1#

您可以通过创建一个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))

相关问题