Home > OS >  Only select id's for which all factors in another column are equal
Only select id's for which all factors in another column are equal

Time:10-13

library(data.table)
test <- fread("id col2
       1    A
       1    A
       2    B
       2    A")
test$col2 <- as.factor(test$col2)

   id col2
1:  1    A
2:  1    A
3:  2    B
4:  2    A

From the data above I would like to simply select all columns for which the factor values in col2 are equal, so only id==1, because the factors are both A. For id==2, the factors are not equal.

How should I do this?

CodePudding user response:

In dplyr, with n_distinct:

library(dplyr)
test %>% 
  group_by(id) %>% 
  filter(n_distinct(col2) == 1)

#   id col2 
# 1  1    A    
# 2  1    A    

In data.table, with uniqueN:

library(data.table)
test[, .SD[uniqueN(col2) == 1], by = id]

#   id col2
#1:  1    A
#2:  1    A
  • Related