Stats
Stats

Reputation: 97

mass removal of rows from a data frame based on a column condition

I would like to remove all rows based on a condition of a column. The code below produces a sample test data

test_data <- data.frame(index = c(1,2,3,4,5), group = c("a", "a", "a", "b", "c"), count = c(1,2,2,3,4))

The data frame has 3 columns: index, group and count. I would like to remove all rows belonging to the same group if any one row of the group has count 1. So in above data frame, I would like to remove entire index 1, 2 and 3 from data frame since first row has count = 1 and row 2nd and 3rd fall in the same group "a". The resultant data frame should look like this:

testdata2 <- data.frame(index = c(4,5), group = c("b", "c"), count = c(3,4))

Any help would be appreciated! Thanks!

Upvotes: 1

Views: 25

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

We can use ave with subset in base R and select groups which has no value where count is 1.

subset(test_data, ave(count != 1, group, FUN = all))

#  index group count
#4     4     b     3
#5     5     c     4

With dplyr this can be done as :

library(dplyr)
test_data %>% group_by(group) %>% filter(all(count != 1))

and data.table

library(data.table)
setDT(test_data)[, .SD[all(count != 1)], group]

Upvotes: 0

Related Questions