Reset cumsum value to zero in R
r
Solution
Here's an approach. Based on `ave`:
transform(dat, count = truth * ave(truth, c(0L, cumsum(diff(truth) != 0)),
FUN = seq_along))
where `dat` is the name of your data frame.
Problem
I have a problem. For example I have this : ``` id truth count 1 1 1 2 1 2 3 0 0 4 1 1 5 1 2 6 1 3 8 0 0 ``` I tried this: ``` fun <- rle(df$truth) df$count <- unlist(sapply(fun$lengths, function(x) {return(1:x)})) ``` But it isn't working.