首页 > 解决方案 > how to determine season dry or rainy in temporal analysis using R?

问题描述

I have the data temporal of temperature, i would like determinate if date be to season dry or rainy.

In my coutry the season dry start in May up to October, and season rainy start in November up to April.

Would be possible create a column with this information in package dplyr ou other?

my data-frame in:

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)

标签: rdplyr

解决方案


One option is to extract the month after converting to Date class, create a condition in case_when to return 'dry', 'rainy' based on the values of 'Month' column

library(dplyr)
library(lubridate)
df <- df %>%
  mutate(Month = month(dmy(Date_dmy)), 
     categ = case_when(Month %in% 5:10 ~ 'dry', TRUE ~ 'rainy'))

推荐阅读