1

I have simplified the two datafrfames:

a = data.frame(ID = c("1", "2", "3", "4", "5"))
b = data.frame(ID = c("1", "1", "6", "7", "8", "8", "3"))

How do I filter data frame b with the matching values in a so only the values of 1:5 will remain in b? So only 1, 1, and 3 will remain in b?

Xin
  • 666
  • 4
  • 16

1 Answers1

0

This can be solved using merge or an inner join. Here are different approaches:

Base R

merge(a, b)
  ID
1  1
2  1
3  3

dplyr

library(dplyr)
inner_join(a, b)

data.table

library(data.table)
setDT(a)[setDT(b), on = "ID", nomatch = 0L]
Uwe
  • 41,420
  • 11
  • 90
  • 134