r - 按模式反连接
问题描述
TibbleA
包含可能在 tibble 中找到的键/模式B
。我的目标是识别在 tibble 中找不到的键/模式B
。
我想通过匹配模式反连接两个表:
A <- tibble( colA = c("B12", "B19", "B202", "B87", "B61", "B55") )
B <- tibble( colB = c("aaB87aa", "bbbbB55", "B202cccc") )
我发现该功能fuzzyjoin
应该可以工作,但我无法正确使用它:
test <- regex_anti_join(A, B, by = c(colB = "colA"))
编辑
最后我想要这样的东西:
test
1 B12
2 B19
3 B61
解决方案
regex_anti_join(A, B, by = c(colA = colB))
执行 a str_detect
of colB
inside colA
。在您的情况下,这不返回任何内容,因此结果为空。
你需要做相反的事情:colA
在里面搜索colB
。
这可以通过将匹配函数指定给以下fuzzy_join
函数使用的函数来实现regex_anti_join
:
library(fuzzyjoin)
# This is exactly the inverse of the match_fun used inside regex_anti_join
match_fun <- function(v1, v2) {
stringr::str_detect(v2, stringr::regex(v1))
}
fuzzy_join(A, B, by = c("colA" = "colB"), match_fun=match_fun, mode = 'anti')
#> # A tibble: 3 x 1
#> colA
#> <chr>
#> 1 B12
#> 2 B19
#> 3 B61
由reprex 包于 2020-07-17 创建(v0.3.0)
推荐阅读
- java - 如何在kotlin中重构超过6个参数的方法
- python - 如何在 Dataframe 中找到最长的 NaN 序列?
- javascript - MongoClient GetData 例程的 NodeJS ASync 调用
- c++ - (Unreal) VS 2019 找不到 GameFramework 文件夹,还有很多其他错误
- javascript - NodeJS 服务器外部访问
- flutter - Flutter 图像生成器回调问题
- reactjs - AuthError - 错误:未正确配置 Amplify
- python - Django Plotly Dash
- javascript - 将二维数组中的不同值推送到唯一数组
- sql-server - 无法删除非对称密钥,因为有一个登录映射到它