library(tidyverse)
library(lubridate)
Consider this sample data:
# A tibble: 36 x 2
date co2
<date> <int>
1 2022-01-01 62
2 2022-01-02 59
3 2022-01-03 55
4 2022-02-01 90
5 2022-02-02 66
6 2022-02-03 74
7 2022-03-01 104
8 2022-03-02 103
9 2022-03-03 74
10 2022-04-01 70
# ... with 26 more rows
# i Use `print(n = ...)` to see more rows
df %>%
mutate(season = case_when(month(date) %in% c(12, 1, 2) ~ "Winter",
month(date) %in% c(3, 4, 5) ~ "Spring",
month(date) %in% c(5, 7, 8) ~ "Summer",
TRUE ~ "Autumn"))
# A tibble: 36 x 3
date co2 season
<date> <int> <chr>
1 2022-01-01 62 Winter
2 2022-01-02 59 Winter
3 2022-01-03 55 Winter
4 2022-02-01 90 Winter
5 2022-02-02 66 Winter
6 2022-02-03 74 Winter
7 2022-03-01 104 Spring
8 2022-03-02 103 Spring
9 2022-03-03 74 Spring
10 2022-04-01 70 Spring
# ... with 26 more rows
# i Use `print(n = ...)` to see more rows
Specify yourself
df %>%
mutate(season = case_when(between(date, as.Date("2022-01-01"),
as.Date("2022-02-01")) ~ "Winter",
TRUE ~ "Not winter"))