there must be a simple solution to the following problem but I cannot figure it out by myself and my searches were not successful unfortunately:
In a data.frame
with multiple columns:
x <- data.frame(id = c(1, 1, 2, 2),
dat = c("a", "b", "a", "b"),
val = 1:4)
I want to filter out only those observations that I have in another data.frame
:
y <- data.frame(id = 1:2,
dat = c("a", "b"))
The result should look like this:
> z
id dat val
1 1 a 1
2 2 b 4
The following approach doesn't lead to the desired result:
> x %>% filter(id %in% y$id & dat %in% y$dat)
id dat val
1 1 a 1
2 1 b 2
3 2 a 3
4 2 b 4
Can someone please point me into the right direction?
Thanks a lot in advance!