Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to eliminate the continuous values in a data table. What I want is to remove all rows of every variable if there are more than 2 zeros in the column. 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 desired result should have data like:

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))

1 Answer

0 votes
by (108k points)

For achieving your goal, you need to use rle(), as it is very simple and easy to use:

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 are a beginner and want to know more about R then do check out the R programming tutorial.

Browse Categories

...