如何在 R 中查找两个大小不等的向量之间的字符串匹配?

问题描述 投票:0回答:1

我有两个带有字符串的向量,如下所示:

x <- c("Zimbabwe (Rhodesia)", "India", "Equatorial Guinea", "United States")

y <- c("Zimbabwe", "India", "Guinea")

我想获得一个向量,其中包含 x

y
之间不
完全
匹配的字符串。理想的结果是:

"Zimbabwe (Rhodesia)" "Zimbabwe" "Equatorial Guinea" "Guinea" "United States"

我试图在这里巩固一些概念 - 我知道我可以使用

grep(paste(y, collapse = "|")
来获取向量的部分匹配,并使用 anchors 来查找给定字符串的精确匹配。但如何整合它们呢?

我该怎么做?

r string variables dplyr tidyverse
1个回答
0
投票

一种选择是使用集合运算:

x <- c("Zimbabwe (Rhodesia)", "India", "Equatorial Guinea", "United States")

y <- c("Zimbabwe", "India", "Guinea")

setdiff(union(x, y), intersect(x, y))
#> [1] "Zimbabwe (Rhodesia)" "Equatorial Guinea"   "United States"      
#> [4] "Zimbabwe"            "Guinea"
© www.soinside.com 2019 - 2024. All rights reserved.