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

Find elements in a column that is not in another column of another dataframe in R

I have two dataframes that look something like this:

dat <- data.frame(col1 = c(1:100))
dat2 <- data.frame(col2 = c(5:105))

I want to find all the elements that are in dat but not in dat2. How can I do this?

Thanks!

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 :

You could use a filtering join, e.g.

dplyr::anti_join(dat,dat2, by = c("col1" = "col2"))

or directly via filter

library(dplyr)
dat %>% filter(!col1 %in% dat2$col2)

Output:

  col1
1    1
2    2
3    3
4    4
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