【发布时间】:2018-10-24 16:50:23
【问题描述】:
所以我试图在另一个数据集中查找每个名称的出现次数。我试图运行的代码是:
Data$Count <- grep(Data$Name,OtherDataSet$LeadName) %>% length()
问题是当我运行它时,名字的数字被映射到该列中的每个位置。为什么会这样?
【问题讨论】:
-
pattern在grep中未矢量化尝试library(stringr); library(dplyr);Data %>% mutate(Count = sum(str_detect(OtherDataSet$LeadName, Name)))假设'Data' 和'OtherDataSet' 具有相同的行数 -
我正在遍历
LeadName中的一列它不起作用,因为它们的行数不同 -
在这种情况下,您可能需要
grep(paste0("\\b(", paste(Data$Name, collapse="|"), ")\\b"), OtherDataSet$LeadName) -
这是做什么的?
-
akrun制作了一个使用正则表达式替代符号|的模式来检查Data$Name中的任何选项是否出现在LeadName中