'Writing a function that restarts cumulative sum whenever a repetition of entries is broken
I am attempting to write a function in R that is similar to cumsum() but only adds up entries that are the same, and restarts the count once the entry changes. So, my data includes a vector of two levels: 0 or 1. I would like to count up the number of entries that all say 1 in a row, and when we get to a 0 the counter should restart back to 1. Hopefully that makes sense, I can clarify if need be.
So I have this fake data:
df <- data.frame(repeats = c(1, 0, 0 , 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1))
I expect my output to be:
1, 3, 4, 2, 1, 1, 2
But I am having much difficulty. I know my code is very wrong. Here is what I have:
cumsum2 <- function(x) {
cumulative <- rep(0, 19)
for (i in cumulative) {
if (i == 1) {
cumulative[i] <- x[[1]] # set first entry as value of x[1]
} else if (i > 1) {
if (x[i] == x[i-1]) { # set all other entries as cumulative sum
sum <- sum(x[i] + x[i-1]) # as long as they match previous entry
cumulative[i] <- sum
} else { # if they don't match previous entry, restart cumsum
cumulative[i] <- x[[i]]
}
}
}
return(cumulative)
}
and my current output is unchanged:
cumsum2(df$repeats)
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Could someone please point me in the right direction? I have not coded for quite some time and I feel like I have forgotten so much, but I am hoping it will start to come back quickly. Thank you in advance.
Solution 1:[1]
You can use rle
rle(df$repeats)$lengths
Output:
[1] 1 3 4 2 1 1 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | langtang |
