【发布时间】:2016-01-22 07:41:20
【问题描述】:
我有一列字符 names,其中包含以下内容:
Raymond K
Raymond K-S
Raymond KS
Bill D
Raymond Kerry
Blanche D
Blanche Diamond
Bill Dates
我还有一个字符向量 m_names 包含以下内容:
Raymond K
Blanche D
我想创建一个列outcome,如果有匹配的子字符串则返回一个非零整数,如果没有匹配则返回0。例如,对于上面的文本列,我理想情况下希望看到结果
[1] 1 1 1 0 1 2 2 0
目前,我已经尝试了以下代码:
outcome <- pmatch(as.character(names), m_names, nomatch = 0)
但这只会返回以下outcome:
[1] 1 0 0 0 1 2 0 0
如何确保即使没有完全匹配,代码仍会返回一个标识 R 中部分匹配的值?
【问题讨论】: