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

Only leave duplicated rows in a dataframe, with R

I have a dataframe that looks like this:

col1 col2 col3
tn1 a b
tn1 a c
tn2 d b
tn3 a b

And I want to leave only those rows that are duplicated for col1 & col2, keeping BOTH rows:

col1 col2 col3
tn1 a b
tn1 a c

I’ve been trying to do this by using unique() or distinct() or anti_join() but can’t figure it out.

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

>Solution :

Base R:

df[df$col1 %in% df$col1[duplicated(df$col1)],]
  col1 col2 col3
1  tn1    a    b
2  tn1    a    c
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