[英]filtering a tibble based on values in a second tibble
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.我想创建 df3,其中包含在 df2$A 中找到 df1$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.但这仅返回 2 行,因为它匹配行,而不是搜索值。 My real data set is several thousand rows.
我的真实数据集是几千行。
Thanks in advance!提前致谢!
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)
The proper way to do this is to use a semi_join()
正确的方法是使用
semi_join()
Eg,例如,
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)由代表 package (v0.3.0) 于 2020 年 5 月 6 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.