Reputation: 485
Given the following tibbles:
df1<- tibble(A = c(1:10), B=sample(c(21:30)))
df2<-tibble(A = c(1,2,4,6,7))
I want to create df3 which contains all the rows in which df1$A is found in df2$A. I've tried
df3<- df1 %>% filter(A == df2%A))
but this returns only 2 rows, because it is matching the rows, not searching for the values. My real data set is several thousand rows.
Thanks in advance!
Upvotes: 0
Views: 319
Reputation: 10222
The proper way to do this is to use a semi_join()
E.g.,
library(tidyverse)
set.seed(123)
df1 <- tibble(A = c(1:10), B = sample(c(21:30)))
df2 <- tibble(A = c(1, 2, 4, 6, 7))
df3 <- semi_join(df1, df2, by = "A")
df3
#> # A tibble: 5 x 2
#> A B
#> <int> <int>
#> 1 1 23
#> 2 2 30
#> 3 4 28
#> 4 6 29
#> 5 7 21
Created on 2020-05-06 by the reprex package (v0.3.0)
Upvotes: 0
Reputation: 5798
library(tidyverse)
df1<- tibble(A = c(1:10), B=sample(c(21:30)))
df2<-tibble(A = c(1,2,4,6,7))
df1 %>%
filter(df1$A %in% df2$A)
Upvotes: 1