Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 多年数据集中一年内的不同季节_R_Dplyr_Tidyverse - Fatal编程技术网

R 多年数据集中一年内的不同季节

R 多年数据集中一年内的不同季节,r,dplyr,tidyverse,R,Dplyr,Tidyverse,我有一个为一个ID分配了多年的数据集。我正在尝试在一年内为每个ID按夏季月份和冬季月份过滤数据。例如,我只想获得ID 1的夏季和冬季月份,分别是2010年、2011年、2012年和2013年 在实际数据集中,每个ID在数据集中的年数不同 以下是我到目前为止的情况: library(lubridate) library(tidyverse) date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"

我有一个为一个ID分配了多年的数据集。我正在尝试在一年内为每个ID按夏季月份和冬季月份过滤数据。例如,我只想获得ID 1的夏季和冬季月份,分别是2010年、2011年、2012年和2013年

在实际数据集中,每个ID在数据集中的年数不同

以下是我到目前为止的情况:

library(lubridate)
library(tidyverse)

date <- rep_len(seq(dmy("01-01-2010"), dmy("31-12-2013"), by = "days"),1000)
ID <- rep(seq(1, 5), 100)

df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000),
                 ID)

#Filters for the core summer months
summer <- df %>% arrange(ID, date) %>%
  filter(month %in% 06:08) 
# Filters for the core winter months
winter <- df %>%  arrange(ID, date) %>%
  filter(month %in% c(01,02,03)) 
库(lubridate)
图书馆(tidyverse)

日期我们可以创建一个新列'season_categ',并将其与'year'和'ID'一起用作分组变量

library(dplyr)
df1 <- df %>%
     mutate(season_categ = case_when(month %in% 6:8 ~ 'summer',
         month %in% 1:3 ~ 'winter')) %>%
     group_by(ID, year, season_categ) 
库(dplyr)
df1%
突变(季节=情况)发生时(月百分比在%6:8~‘夏季’,
月份%1:3~‘冬季’)%>%
分组依据(ID、年份、季节类别)