I have a huge df and trying to execute follow code takes too long. Anyway to speed it up?
df$col2 <- 0
for (i in 2:nrow(df)) {
if (df$col1 > 0) {
df$col2[i] <- df$col2[i-1] + 1
}
else {
df$col2[i] <- 0
}
}
Example data
df <- data.frame(col1 = c(1, 0, 10, 28, 0, 0, 2))
Expected result
col1 col2
1 1
0 0
10 1
28 2
0 0
0 0
2 1
I am trying to use col2
to count cumulative non-zero variables in col1
and reset the count when I hit a 0
in col1
.