R多重模糊匹配agrep创建变量

时间:2014-08-06 20:17:30

标签: r match agrep

R的新功能。我想通过创建一个变量(是/否)来创建测试,该变量检查名字或姓氏是否与电子邮件地址模糊匹配。如果是这样,请在该行附加一个“是”变量。

数据示例:

id firstname lastname email address match
1 patrick boyles patrickb@gmail.com yes
2 zeke cosmos zeke@gmail.com yes
3 foo foo abcd@gmail.com no

我明白我需要使用agrep。令我困惑的是如何告诉R检查2列(名字和姓氏)并且只检查该行。

由于 - 新手

1 个答案:

答案 0 :(得分:2)

这是从

开始的
library(stringdist) # install.packages("stringdist") b4, if you need to
df <- read.table(header = TRUE, text = "id firstname lastname emailaddress match
1 patrick boyles patrickb@gmail.com yes
2 zeke cosmos zeke@gmail.com yes
3 foo foo abcd@gmail.com no")
df$match2 <- ifelse(with(df, stringdist(a = paste0(firstname, lastname), 
                                        b = sub("(.*)@.*", "\\1", emailaddress), 
                                        method = "lcs")) <= 7, 
                    "yes", "no")
df
#   id firstname lastname      email.address match match2
# 1  1   patrick   boyles patrickb@gmail.com   yes    yes
# 2  2      zeke   cosmos     zeke@gmail.com   yes    yes
# 3  3       foo      foo     abcd@gmail.com    no     no