filter duplicated rows that have nonmatching values in r

We can do a group by operation and filter

library(dplyr)
df %>% 
     group_by(id) %>%
     filter(n() > 1 & cat != actual.cat|n() == 1)

-output

# A tibble: 6 x 3
# Groups:   id [6]
#     id   cat actual.cat
#  <dbl> <dbl>      <dbl>
#1     1     3          3
#2     2     3          4
#3     3     5          5
#4     4     2          2
#5     5     2          1
#6     6     5          7

Or using base R

subset(df, id %in% names(which(table(id) > 1)) & 
     cat != actual.cat| id %in% names(which(table(id) == 1)))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top