Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Drop rows which are duplicates regarding certain columns

I want to identify and remove observations which are duplicates in certain aspects.

In my example, I want to get rid of rows 1 and 6, as they are the same in both V1 and V2. That they differ in V3 shouldn’t matter.

df <- data.frame(V1 = c("a","b","c","a","c","a"),
                 V2 = c(1,2,1,2,3,1),
                 V3 = c(1,2,3,4,5,6))

Applying dplyr::distinct(df, V1, V2) results in row 6 being discarded while row 1 remains. As I said, I want both rows 1 and 6 removed. I am sure the problem is trivial, but I can’t think of the correct search terms …

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

We can group-by then filter:

group_by(df, V1, V2) %>%
  filter(n() == 1) %>%
  ungroup()
# # A tibble: 4 × 3
#   V1       V2    V3
#   <chr> <dbl> <dbl>
# 1 b         2     2
# 2 c         1     3
# 3 a         2     4
# 4 c         3     5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading