Skip to content

Instantly share code, notes, and snippets.

@tbobin
Last active January 21, 2018 18:44
Show Gist options
  • Save tbobin/f7c2703e478f5b39a4c4daaa53eafc06 to your computer and use it in GitHub Desktop.
Save tbobin/f7c2703e478f5b39a4c4daaa53eafc06 to your computer and use it in GitHub Desktop.
rolling count repeating 1 in column
library(tidyverse)
dates <- c("2017-10-30",
"2017-11-06",
"2017-11-14",
"2017-11-20",
"2017-11-27",
"2017-12-04",
"2017-12-11",
"2017-12-18",
"2018-01-08")
waiting <- c(0, 1, 1, 0, 0, 0, 1, 1, 1)
df <- data.frame(dates = as.Date(dates), waiting = waiting)
df %>% mutate(c_sum = sequence(rle(waiting)$lengths)*waiting)
# repeating same conneced values
a <- rle(waiting)
sequence(a$lengths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment