I have a dataframe, which contains dates of many years. It looks like this (instead of three years, I have 40 years):
DATES<-c(seq(as.Date('2017-01-01'), as.Date('2019-12-31'), by = 'days'))
df<-data.frame(DATES)
for each day I want to add the season. Hereby, Spring should start at the 20th of March, Summer at the 21st of June, Autumn at the 23rd of September, Winter at the 21st of December. These dates stay unchanged over the years.
I came up with the following code, which works (at least, I think so). However, I was wondering, if there isn't are more elegant way, to get what I want.
df$MONTH<-month(df$DATES)
df$DAY<-mday(df$DATES)
df$DAY_PLUS_MONTH<-df$DAY+df$MONTH*100
df <- df %>%
mutate(SEASON = case_when(
DAY_PLUS_MONTH %in% 320:620 ~ 'SPRING',
DAY_PLUS_MONTH %in% 621:922 ~ 'SUMMER',
DAY_PLUS_MONTH %in% 923:1221 ~ 'AUTUMN',
TRUE ~ 'WINTER'))