如何使用 R 从多时态数据列表中确定一年中的季节?

How to determine the seasons of the year from a multitemporal data list using R?

我想使用 dplyr 或 tidyr 从时间表中确定我所在地区的季节。

我省:

夏季:从 12 月 21 日开始到 3 月 20 日。 秋季:从 3 月 21 日开始到 6 月 20 日。 冬季:从 6 月 21 日开始到 9 月 22 日。 Spring:9 月 23 日至 12 月 20 日开始。

我的data.frame

sample_station <-c('A','A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B','C','C','C','C','C','C','C','C','C','C','A','B','C','A','B','C')
Date_dmy <-c('01/01/2000','08/08/2000','16/03/2001','22/09/2001','01/06/2002','05/01/2002','26/01/2002','16/02/2002','09/03/2002','30/03/2002','20/04/2002','04/01/2000','11/08/2000','19/03/2001','25/09/2001','04/06/2002','08/01/2002','29/01/2002','19/02/2002','12/03/2002','13/09/2001','08/01/2000','15/08/2000','23/03/2001','29/09/2001','08/06/2002','12/01/2002','02/02/2002','23/02/2002','16/03/2002','06/04/2002','01/02/2000','01/02/2000','01/02/2000','02/11/2001','02/11/2001','02/11/2001')
Temperature <-c(17,20,24,19,17,19,23,26,19,19,21,15,23,18,22,22,23,18,19,26,21,22,23,27,19,19,21,23,24,25,26,29,30,21,25,24,23)

df<-data.frame(sample_station, Date_dmy, Temperature)

1) 使用 findInterval 在 season_start 向量中查找日期并提取关联的 season_name.

library(dplyr)

# given Date class vector returns vector of season names
date2season <- function(date) {
  season_start <- c("0101", "0321", "0621", "0923", "1221") # mmdd
  season_name <- c("Summer", "Autumn", "Winter", "Spring", "Summer")
  mmdd <- format(date, "%m%d")
  season_name[findInterval(mmdd, season_start)] ##
}

df %>% mutate(season = date2season(as.Date(Date_dmy, "%d/%m/%Y")))

给予:

   sample_station   Date_dmy Temperature season
1               A 01/01/2000          17 Summer
2               A 08/08/2000          20 Winter
3               A 16/03/2001          24 Summer
4               A 22/09/2001          19 Winter
5               A 01/06/2002          17 Autumn
...snip...

1a) date2season 中的最后一行标记为##,可选择替换为

season_name[(mmdd >= "0101") + (mmdd >= "0321") + (mmdd >= "0621") + 
 (mmdd >= "0923") +  (mmdd >= "1221")]

在这种情况下,您也不需要定义 season_start 的行。

2) 另一种方法是使用 case_when:

df %>%
  mutate(mmdd = format(as.Date(Date_dmy, "%d/%m/%Y"), "%m%d"),
         season = case_when(
           mmdd <= "0320" ~ "Summer",
           mmdd <= "0620" ~ "Autumn",
           mmdd <= "0922" ~ "Winter",
           mmdd <= "1220" ~ "Spring",
           TRUE ~ "Summer")) %>%
  select(-mmdd)