How to identify duplicate id by two columns

Hi RStudio Community, I would like to list duplicate ids by two columns (id and date1) below if somebody can help me. It is highly appreciate it. Thanks
r

# identify duplicates by two columns
data <- data.frame(id = c(1L,2L,2L,3L,3L,4L,5L,6L,6L,7L),
                    date1 = c("2020-01-25", "2021-03-15","2021-03-15","2021-05-11","2021-05-11","2020-06-07","2021-08-08", "2020-10-18","2020-10-18", "2021-11-11"),
                   x = factor(c("B", "B", "A", "F", "A", "B", "A", "B","A", "B")),
                   stringsAsFactors = FALSE)

#duplicates
data$id[duplicated(data$id)]
sum(duplicated(data[,1:2]))

How about this?

library(tidyverse)
data %>% 
  count(id, date1) %>% 
  filter(n > 1)

  id      date1 n
1  2 2021-03-15 2
2  3 2021-05-11 2
3  6 2020-10-18 2


Thank you very much williaml. It helped.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.