Count consecutive values in groups with condition with dplyr and rle

半世苍凉 提交于 2019-12-08 01:20:31

问题


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.


回答1:


We need to create a logical index with lengths and get the sum of it

df %>%
   group_by(Era, Year) %>% 
   do({ tmp <- with(rle(.$consec), sum(lengths > 2))
   data.frame(Count = tmp)})
#   Era  Year Count
#  <dbl> <dbl> <int>
#1     1     1     0    
#2     1     2     0
#3     1     3     1


来源:https://stackoverflow.com/questions/40083160/count-consecutive-values-in-groups-with-condition-with-dplyr-and-rle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!