0
votes

I have the following data:

 dat2<-structure(list(year = c(1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L,1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 1979L, 
 1979L, 1979L, 1979L, 1979L, 1979L), mon = c(5L, 5L, 5L, 5L, 5L, 
 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
 7L, 7L, 7L, 7L, 7L, 7L, 7L), day = c(1L, 2L, 3L, 4L, 5L, 6L, 
 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 
 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
 26L, 27L, 28L, 29L, 30L, 31L), phase = c(2L, 3L, 3L, 3L, 3L, 
 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 
 8L, 8L, 8L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
 3L, 3L, 4L, 4L, 4L, 4L, 5L), Rainfall = c(0, 0.25, 0, 0, 0, 0, 
 0, 0, 0, 0, 0, 19.2, 11.125, 1.95, 0.125, 23.2, 35.575, 37.4, 
 6.425, 10.275, 3.05, 50.075, 23.05, 2, 1.4, 3.325, 5.8, 13.375, 
 27.725, 14.3, 20.9, 5.075, 11.5, 0.825, 0.9, 0.95, 1, 0.075, 
 0.025, 1.15, 0.325, 0, 0, 0.325, 1.925, 2.15, 6.55, 3.15, 2.15, 
 1.725, 0.575, 4.875, 3, 3.6, 3.95, 14.35, 7.625, 9.2, 9.275, 
 18.375, 6.525, 0.36, 0.1, 75.04, 38.56, 1.18, 1.16, 4.12, 5.7, 
 5, 0, 1.36, 0, 5.18, 0.64, 2.68, 0.36, 0.3, 0, 3.56, 9.62, 0.52, 
 1.26, 17.04, 16.3, 2.84, 10.2, 52.98, 51.76, 15.06, 19.62, 19.46
 )), row.names = c(NA, 92L), class = "data.frame")

There are four columns (Year, Month, Day, Phase, and Rainfall) in this data set.

I would like to count the number of times that:

(1) the "Rainfall" is below 5 mm/day for at least 3 consecutive days

(2) The phase is "Phase 1"

I am not sure how to apply correctly the RLE function for this. So far, I have the following script but does not contain the second condition:

dat2<-dat[,c("phase","Rainfall")]
countruns = function(x){
RLE = rle(x$Rainfall<5)   
sum(RLE$lengths==1)
}

The sum() should give the total counts satisfying the 2 conditions above.

I'll appreciate any help on this.

Lyndz

2
"this is the shortest data set that I can provide" -- why exactly is that? What prevents you from posting just a representative sample from this data, directly in the question itself? If you haven't done so already, please read How to make a great R reproducible example?John Coleman
I edited the post. Thanks for the suggestion.Lyndz
What do you mean by "consecutive" in your description? If I subset the sample data to leave only observations where phase == 1, The date 1979-06-04 is preceded by 1979-06-03, but proceeded by 1979-07-12. Does the >1 month gap matter?Feakster
Should be consecutive days. If there is a gap, then it should not be counted.Lyndz

2 Answers

1
votes

How about an approach where you first subset to phase == 1 and then Rainfall below 5 mm/day for at least 3 consecutive days.

# Subset to phase == 1
dat_p1 <- subset(dat2, phase == 1)

# Find cases where Rainfall is below 5 mm/day for at least 3 consecutive days
rainfall_rle_df <- data.frame(unclass(rle(dat_p1$Rainfall < 5)))
nrow(subset(rainfall_rle_df, lengths >= 3 & values == TRUE))

#----
[1] 1

1
votes

Does this achieve what you want?

library(tidyverse)

dat_out <- dat2 %>%
  mutate(phase1_T = phase == 1,
         rainfall_T = Rainfall < 5 & lag(Rainfall, n = 1, default = 5) < 5 & lag(Rainfall, n = 2, default = 5) < 5,
         both_conditions = phase1_T & rainfall_T)

sum(dat_out$both_conditions)

If you need to put this inside a function as requested in comments, you can do the following:

library(tidyverse)

contruns <- function(.df){
  dat_out <- .df %>%
    mutate(phase1_T = phase == 1,
           rainfall_T = Rainfall < 5 & lag(Rainfall, n = 1, default = 5) < 5 & lag(Rainfall, n = 2, default = 5) < 5,
           both_conditions = phase1_T & rainfall_T)
  
  sum(dat_out$both_conditions)
}

contruns(dat2)