Home > Software engineering >  Find elements in a column that is not in another column of another dataframe in R
Find elements in a column that is not in another column of another dataframe in R

Time:05-18

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!

CodePudding user response:

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

CodePudding user response:

You can use setdiff:

setdiff(dat$col1, dat2$col2)
#[1] 1 2 3 4

CodePudding user response:

Option using data.table:

library(data.table)
setDT(dat)
setDT(dat2)
dat[!dat2, on = .(col1 = col2)]

Output:

   col1
1:    1
2:    2
3:    3
4:    4
  • Related