library(dplyr)
library(purrr)
id = rep(letters[1:10], each = 10)
t = rep(1:10, times = 10)
dummy = sample(x = c(0, 1), size = 100, replace = T)
df <- tibble(id = id, t = t, dummy = dummy)
x <- df$dummy[1:10]
out <- vector(mode = "numeric", length = length(x))
j <- 1
out[1] <- j
for(i in 2:10){
out[i] <- if(x[i-1] == x[i]){
j
}else{
j <- j + 1
j
}
}
tib <- tibble(x,out)
check_seq <- function(x){
out <- vector(mode = "numeric", length = length(x))
j <- 1
out[1] <- j
for(i in 2:length(x)){
out[i] <- if(x[i-1] == x[i]){
j
}else{
j <- j + 1
j
}
}
out
}
df2 <- df %>% group_by(id) %>% mutate(newcol = check_seq(dummy)) %>% add_count(newcol)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.