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))
Using rle :
library(data.table)
dt[!with(rle(a == 0), rep(values * lengths > 2, lengths))]
# a b c
#1: 1 x 2
#2: 2 y 5
#3: 1 x 1
#4: 1 y 3
#5: 2 y 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With