Использование data.table
:
library(data.table)
setDT(df)
df[, .(nacc = uniqueN(account_type)), by = account][nacc > 1, toString(account)]
## [1] "1, 3"
Делать что-то подобное в dplyr
:
library(dplyr)
df %>%
group_by(account) %>%
summarise(nacc = n_distinct(account_type)) %>%
filter(nacc > 1) %>%
summarise(toString(account)) %>%
pull()
## [1] "1, 3"
Где:
df <- data.frame(
account = c(1L, 1L, 1L, 2L, 3L, 3L),
account_type = c("A", "B", "C", "A", "C", "D")
)