jo_
jo_

Reputation: 731

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!

Upvotes: 2

Views: 4680

Answers (3)

Quinten
Quinten

Reputation: 41225

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

Upvotes: 3

Ma&#235;l
Ma&#235;l

Reputation: 51894

You can use setdiff:

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

Upvotes: 4

Julian
Julian

Reputation: 9240

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

Upvotes: 5

Related Questions