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
phase == 1
, The date1979-06-04
is preceded by1979-06-03
, but proceeded by1979-07-12
. Does the >1 month gap matter? – Feakster