A cold wave is defined if the minimum temperature at a meteorological station is below the normal temperature by 3 °C or more consecutively for 3 days or more. I have tried to calculate it multiple stations using following code
#Calculation for multistation
set.seed(123)
df <- data.frame("date"= seq(from = as.Date("1970-1-1"), to = as.Date("2000-12-31"), by = "day"),
"Station1" = runif(length(seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days")), 10, 30),
"Station2" = runif(length(seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days")), 11, 29),
"Station3" = runif(length(seq.Date(as.Date("1970-1-1"), as.Date("2000-12-31"), "days")), 9, 28))
head(df)
df$day <- format(df$date, format='%m-%d')
#Daily average (daily normal) calculation
df_summarise_all <- df %>%
as_tibble() %>% # for easier viewing
mutate(day = format(df$date, format='%m-%d')) %>%
group_by(day) %>%
summarise_all(funs(mean)) %>%
pivot_longer(cols = -c(date, day), names_to = "variable", values_to = "value")
#Coldwave event calculation
df %>%
as_tibble() %>% # for easier viewing
pivot_longer(cols = -c(date, day), names_to = "Stations", values_to = "MinT") %>%
left_join(df_summarise_all %>% rename(mean_MinT = value), by = 'day') %>%
mutate(is_coldwave = zoo::rollapplyr(MinT < (mean_MinT - 3),
3, all,fill = NA))
As can be seen from the output, the joining of the daily normal and station minimum temperature is not correct. I have three questions
- How to correct the join?
- How to get the count of coldwaves per year for every station? and
- How to get overall coldwave count for every station?