How to filter a dataframe based on the duplicated values?

I have this df

df = data.frame(x = c(1,1,2,2,3,4),
                y = LETTERS[1:6] )

The desired output is

  x y
1 1 A
2 1 B
3 2 C
4 2 D

I tried using the filter finction but I haven’t got the result I am looking for.

Thanks.

>Solution :

You can use n() by group:

library(dplyr) #1.1.0 needed or above
df %>% 
  filter(n() > 1, .by = x)

  x y
1 1 A
2 1 B
3 2 C
4 2 D

Or, in base R:

subset(df, ave(x, x, FUN = length) > 1)

And in data.table:

setDT(df)[, if(.N > 1) .SD, x]

Leave a Reply