My question is very similar to the one posed below, however I want to add an additional command to return only cases when a sequence has more than 2 consecutive values.
How do I count the number of consecutive "success" (i.e. 1 in $consec) when a given sequence run has more than 2 consecutive numbers, within a given Era and a given Year?
Similar question to: Summarize consecutive failures with dplyr and rle . For comparison, I've modified the example used in that question:
library(dplyr)
df <- data.frame(Era=c(1,1,1,1,1,1,1,1,1,1),Year = c(1,2,2,3,3,3,3,3,3,3), consec = c(0,0,1,0,1,1,0,1,1,1))
df %>%
group_by(Era,Year) %>%
do({tmp <- with(rle(.$consec==1), lengths[values])
data.frame(Year= .$Year, Count=(length(tmp)))}) %>%
slice(1L)
> Source: local data frame [3 x 3]
> Groups: Era, Year
> Era Year Count
> 1 1 1 0
> 2 1 2 1
> 3 1 3 2
>
All I need now is to add a condition to include only cases of consecutive numbers in a sequence of >2. Desired result:
> Source: local data frame [3 x 3]
> Groups: Era, Year
> Era Year Count
> 1 1 1 0
> 2 1 2 0
> 3 1 3 1
Any advice would be greatly appreciated.