0

Assume that I have this data set

enter image description here

I would like this result

enter image description here

I am not sure how to do this, but here is my pseudocode

problem = if_else(problem == 0, 0, sum of all previous 1s)

I have tried

dataset <- dataset %>% mutate(problem = if_else(problem == 0, 0, problem + lag(problem))

That will only get me a 2 as the highest number. How would I accomplish this. Also, the column name for the total could be different from problem as well.

Mark
  • 65
  • 3
  • Relevant: [R cumulative sum by condition with reset](https://stackoverflow.com/questions/32994060/r-cumulative-sum-by-condition-with-reset) – markus May 03 '19 at 19:27

1 Answers1

1

Here is an option with data.table. Convert to data.table (setDT), grouped by the rleid (run-length-id) of 'Problem', multiply the 'Problem' with the row number (seq_len(.N)) and assign (:=) it to 'Problem'

library(data.table)
setDT(df1)[, Problem := seq_len(.N) * Problem, rleid(Problem)]
df1
#    Problem
# 1:       0
# 2:       1
# 3:       2
# 4:       3
# 5:       0
# 6:       0
# 7:       1
# 8:       2
# 9:       0
#10:       0
#11:       1

Or using base R

with(rle(df1$Problem), sequence(lengths) * rep(values, lengths))

data

df1 <- data.frame(Problem = c(0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1))
akrun
  • 874,273
  • 37
  • 540
  • 662