As I understand it, you want a column that checks if the temperature deviates by 5 – 6 degrees (if the temp is greater than 10) and another column to check if it is differs by 7 degrees or more.
The current code you are using seems to identify the coldwave
values correctly, although is including the severe_coldwave
values as well.
You could add another check for the severe weather, similar to what is already coded, and set any is_coldwave
values to FALSE
if the weather is severe.
This will set values that deviate from 5-6.5 degrees to is_coldwave
and more than 6.5 to is_severe_coldwave
df_out <- df %>%
as_tibble() %>% # for easier viewing
mutate(day = format(as.Date(df$Date, format="%Y-%m-%d"), format="%m-%d")) %>%
tidyr::pivot_longer(cols = -c(Date, day),
names_to = "Stations", values_to = "MinT") %>%
left_join(df_summarise_all %>% rename(mean_MinT = value),
by = c('day' = 'day', 'Stations' = 'variable')) %>%
mutate(is_coldwave = MinT < ifelse(mean_MinT < 10, mean_MinT - 4, mean_MinT - 5))%>%
mutate(is_severe_coldwave = MinT <= ifelse(mean_MinT < 10, mean_MinT - 5.5, mean_MinT - 6.5 ##values differing by 7 or more are is_severe_coldwave
)) %>%
mutate(is_coldwave= ifelse(is_severe_coldwave == T, F, is_coldwave)) ##values labeled is_coldwave and is_severe_coldwave updated so that is_coldwave = F
10
solved How to calculate a condition using dplyr?