简体   繁体   English

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

[英]R multiple fuzzy match agrep create variable

New to R. I would like to create a test by creating a variable (yes/no) that checks to see if first name OR last name fuzzy match to email address. R的新手。我想通过创建一个变量(是/否)来创建测试,以检查名字或姓氏是否与电子邮件地址模糊匹配。 If so, append a 'yes' variable to that row. 如果是这样,请将“ yes”变量附加到该行。

Data Example: 数据示例:

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

I understand that I need to use agrep. 我了解我需要使用agrep。 What confuses me is how to tell R to check 2 columns (first name and last name) and only check within that row. 令我困惑的是如何告诉R检查2列(名字和姓氏)并且仅在该行中检查。

Thanks -The newbie 谢谢-新手

Here is something to start with 从这里开始

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM