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

Filter rows with common entry in two columns

I have the following mock data:

df <- data.frame(Col1=c("cat","dog","man","man","cat","cat"),
                 Col2=c("cat","dog","dog","dog","dog","cat"))

I want to filter out the rows which have the same name in both columns. In other words, I want to be left with unique names across each row.

So in my example, I will be left with an output like:

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

Col1 Col2
man dog
man dog
cat dog

Is there a tidyverse solution for this?

>Solution :

We can use

library(dplyr)
df %>% 
  filter(Col1 != Col2)
  Col1 Col2
1  man  dog
2  man  dog
3  cat  dog

Or using data.table

library(data.table)
setDT(df)[Col1 != Col2]
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