Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

create a cumulative count of adjacent repetitions in a column

$
0
0

I want to create a cumulative count of adjacent repetitions in a column. For instance the desired output for repeat_n in the code below should be c(1,2,3,1,2,1,2), but instead I am getting c(1,2,2,1,2,1,2). Perhaps because case_when() is vectorized, case_when() evaluates all values simultaneously rather sequentially evaluating the updated values. How should I avoid this problem?

library(dplyr)

tibble(x = c(1,1,1,0,0,1,1)) %>% 
  mutate(
    repeat_n = 1, 
    repeat_n = 
      case_when(
        x == lag(x) ~ lag(repeat_n) + 1,
        TRUE ~ repeat_n
      )
  )

Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>