I am trying to remove consecutive values in a data table. So in this case I want to eliminate all rows of every variable if there are more than 2 zeros in column a. So I need something like a maxgap to define how much consecutive zeros are allowed for some flexibility.
Here is an example:
library(data.table)
dt <- data.table(a = c(1, 2, 1, 0, 0, 0, 0, 1, 2),
b = as.factor(c("x", "y", "x", "x", "y", "z", "x", "y", "y")),
c = c(2, 5, 1, 0, 3, 6, 0, 3, 4))
and the result looks like this:
dtRes <- data.table(a = c(1, 2, 1, 1, 2),
b = as.factor(c("x", "y", "x", "y", "y")),
c = c(2, 5, 1, 3, 4))