我有一个数据框(搜索),其中包含句子(在原始地址中),这些地址的甲酸盐非常糟糕。我有一个额外的数据框(查找),其中包含单个单词(在原始城市中),现在我想合并(结果),以便在 A$search 中搜索流行词,并因此添加 ID。 (结果 $Buzzword 列很好,作为控制,但并不重要)。谢谢。
search <-data.frame(A = c("This is a random example3 ", "This is even more radom test2", "Why would text3 this happen", "how can toxt2 this", "complete difference"))
find <- data.frame(Buzzword = c("example3", "test2", "text3"),
ID = c(1, 2, 3)
results <- data.frame(A = c("This is a random example3", "This is even more radom test2", "Why would text3 this happen", "how can toxt2 this"), Buzzword = c("example3", "test2", "toxt2", NA), ID = c(1,2,2))
您可以使用
regmatch
+ match
尝试下面的代码
buzwd <- regmatches(
search$A,
gregexpr(paste0(find$Buzzword, collapse = "|"), search$A)
)
cbind(
search,
find[match(unlist(replace(buzwd, lengths(buzwd) == 0, NA)), find$Buzzword), ]
)