0

I would like to check if I have the same identification (ID) for the 2 groups. For example:

ID   group 
A1    1
A1    1 
A1    1 
A2    2
A2    2
A2    1
A3    3
A3    3
A3    3

Please, notice that I had one ID = A2 associated with group 1 and 2.

My question is: how can I identify this kind of situation in my database using an R code?

I'd like to filter if I have my unique ID contained in two groups.

Thank you!

BD'auria
  • 135
  • 10

1 Answers1

1

We could use n_distinct after grouping by 'ID' to get a logical summarised 'flag'

library(dplyr)
df1 %>% 
   group_by(ID) %>%
   summarise(Flag = n_distinct(group) == 1, .groups = 'drop')
# A tibble: 3 x 2
#   ID    Flag 
#* <chr> <lgl>
#1 A1    TRUE 
#2 A2    FALSE
#3 A3    TRUE 

If we need to filter those rows,

df1 %>%
    group_by(ID) %>% 
    filter(n_distinct(group) > 1) %>%
    ungroup

data

df1 <- structure(list(ID = c("A1", "A1", "A1", "A2", "A2", "A2", "A3", 
"A3", "A3"), group = c(1L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 3L)),
class = "data.frame", row.names = c(NA, 
-9L))
akrun
  • 874,273
  • 37
  • 540
  • 662