【问题标题】:Variable substring match between two column两列之间的可变子字符串匹配
【发布时间】:2017-03-04 09:12:07
【问题描述】:

我有一个包含 20,000 行的数据集,其最纯粹的形式如下所示:

    v1                   v2
1   Case 1 (A v. B)      A v. B 
2   Case 2 (A v. C)      A v. B 
3   Case 2 (A v. C)      C v. B 
4   Case 4 (X v. Z)      X v. Z 
5   Case 5 (B v. A)      A v. B 
6   Case 6 (X v. A)      X v. A 
7   Case 6 (X v. A)      A v. X 
...

...除了 v1, v2 有 n 种变体(实际上大约 150 种左右,但仍然太多,无法列出)。

我想返回第三列 v3,其中包含 v1 的任何子字符串是否与 v2 中的字符串匹配的逻辑指示符。

    v1                   v2           v3
1   Case 1 (A v. B)      A v. B       TRUE
2   Case 2 (A v. C)      A v. B       FALSE
3   Case 2 (A v. C)      C v. B       FALSE
4   Case 4 (X v. Z)      X v. Z       TRUE
5   Case 5 (B v. A)      A v. B       FALSE
6   Case 6 (X v. A)      X v. A       TRUE
7   Case 6 (X v. A)      A v. X       FALSE

我一直在玩这样的事情,我认为这是在正确的轨道上:

library(stringr)
x$v3 <- with(x, str_detect(v1, v2))

如果有人能指出正确的解决方案/解决方法,我将不胜感激。

MWE 显示我的 str_detect() 技术不起作用:

x <- structure(list(v1 = c("Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation", 
                          "Application of the International Convention on the Elimination of All Forms of Racial Discrimination Georgia  v  Russian Federation"
), v2 = c("Georgia v Russian Federation", " Ethiopia v South Africa Liberia v South Africa", 
             " Cameroon v United Kingdom", " New Zealand v France", " Australia v France", 
             " Nicaragua v United States of America", " Nicaragua v Honduras", 
             " Nauru v Anustralia", " Nnew Zealand v France", " Islamic Republic of Iran v United States of America", 
             " Bosnia and Herzegovina v Serbia and Montenegro", " Spain v Cananda", 
             " Libyan Arab Jamahiriya v United States of America", " Libyan Arab Jamahiriya v United Kingdom", 
             " Democratic Republic of the Congo v Burundi", " Germany v United States of America", 
             " Democratic Republic of the Congo v Belgium", " Liechtenstein v Germany", 
             " Democratic Republic of the Congo v Ugandan", " Democratic Republic of the Congo v Rwandan", 
             " Nicaragua v Colombia", " Djibouti v France", " Georgia v Russian Federation", 
             " Croatia v Serbia", " Mexico v United States of American", " Democratic Republic of the Congo v Rwanda", 
             " Spain v  Canada", " Australia v  France", " New Zealand v France", 
             " New Zealand v France")), .Names = c("v1", "v2"
             ), row.names = c(NA, 30L), class = "data.frame")

【问题讨论】:

    标签: r stringr


    【解决方案1】:

    grepl 可用于将 v2 中的单个值与 v1 的可能子字符串进行比较

    您需要分别为每一行应用它,因此快速解决方案可以是: apply(data.frame(v1,v2),MARGIN=1, FUN=function(x) {grepl(x[2],x[1])})

    如果您想忽略空格数量的差异(如第 1 行中的),您可以使用 gsub 将 x[2] 中的值替换为适当的正则表达式,因此 " " 将替换为 @987654324 @ 允许多个空格。

    在这种情况下,此应用将起作用:

    apply(x,MARGIN=1, FUN=function(x) {grepl(gsub(" "," *",x[2]),x[1])})

    【讨论】:

    • 我不认为你是对的。第 1 行和第 23 行中的 v1 在 Georgia 之后和“v”之后包含 2 个空格,它不包含 v2 中的双空格。我将在答案中添加有关空间以及如何解决它们的说明
    • 你能把你用的函数贴在这里吗?也许重新检查您发布的数据?我创建了您在问题中发布的数据框并应用了相同的函数并在 1 和 23 上得到了 TRUE,其他一切都是错误的
    • 我重置了我的记忆,它起作用了 - 谢谢!你为我节省了大量时间。很高兴答案如此简单。我还能够使用 agrep() 函数实现模糊字符串匹配: apply(x, MARGIN=1, FUN=function(x) { agrepl(gsub(" "," *", x[2]), x[ 1], max.distance=.25)})
    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 2021-08-29
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2019-11-27
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多