Get duplicate values within groups

Viewed 133

I'm trying to get duplicate values within groups. More specifically I want to check duplicate Child ID within each household, but duplicate Child ID across different households are fine. For example, I have a data frame called "village":

village <- data.frame(household = c(1, 1, 1, 2, 3, 3), 
                      children = c("A001", "A002", "A001", "A004", "A005", "A001"))

I want to get an output of:

expected <- data.frame(household = c(1, 1), 
                       children = c("A001", "A001"))
2 Answers

We can group by 'household', 'children' and filter the rows where the number of rows is greater than 1

library(dplyr)
village %>% 
   group_by(household, children) %>% 
   filter(n() > 1) %>%
   ungroup

-output

# A tibble: 2 x 2
#  household children
#      <dbl> <chr>   
#1         1 A001    
#2         1 A001   

Or using base R with duplicated

village[duplicated(village)|duplicated(village, fromLast = TRUE),]
#  household children
#1         1     A001
#3         1     A001 

Another base R option using subset + ave

> subset(village,ave(household,household,children,FUN = length)>1)
  household children
1         1     A001
3         1     A001
Related