How can I count the number of consecutive days grouping by year and climate division in Rstudio?

  Kiến thức lập trình

I want to count the number of heatwaves, or consecutive days, for each climate division (CLIMDIV) and year from my dataframe. This dataset is filtered from a complete list of climate divisions and years with temperatures, so that I only have dates that are above 32C. Basically, I’m trying to see how many times in each year in each climate division that there were 2 consecutive days, 3 consecutive days, and 4 consecutive days over 32C, or heatwaves of various lengths.

I’ve tried using rle, but I can’t figure out how to group the consecutive counts by climate division and year, nor how to create a list of different consecutive day counts. I also tried this code, which outputs the format I want, but the result was too small and not accurate when I counted from my list manually.

library(dplyr) 

df18 <- data.frame(
  CLIMDIV = c(101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 102,
              102, 102, 102, 102, 102, 102),
  Year = c(1970, 1970, 1970, 1970, 1970, 1970, 1971, 1971, 1971, 1971, 1971, 1970, 1970, 1970,
           1972, 1972, 1972, 1972),
  Date = as.Date(c("1970-01-01", "1970-01-02", "1970-01-03", "1970-01-06", "1970-01-07", "1970-01-09",
                   "1971-01-01", "1971-01-03", "1971-01-04", "1971-01-05", "1971-01-06",
                   "1970-01-01", "1970-01-02", "1970-01-03",
                   "1972-01-01", "1972-01-02", "1972-01-03", "1972-01-04")),
  Max_Heat_Index = c(32.2, 32.3, 32.4, 32.5, 32.6, 32.7, 32.8, 32.9, 33.0, 33.1, 33.2, 33.3,
                     33.4, 33.5, 33.6, 33.7, 33.8, 33.9)
)

# 2 day heatwaves
grouped_data2day <- df18 %>% 
  group_by(CLIMDIV, Year) %>%
  mutate(Consecutive = cumsum(c(1, diff(Date) != 1))) %>%
  group_by(CLIMDIV, Year, Consecutive) %>%
  summarize(Count = n()) %>%
  filter(Count >= 2) %>%
  summarise(days2over32C = n()) 

# 3 day heatwaves
grouped_data3day <- df18 %>% 
  group_by(CLIMDIV, Year) %>%
  mutate(Consecutive = cumsum(c(1, diff(Date) != 1))) %>%
  group_by(CLIMDIV, Year, Consecutive) %>%
  summarize(Count = n()) %>%
  filter(Count >= 3) %>%
  summarise(days3over32C = n()) 

New contributor

Summer Olsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT