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:
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]