在逗号分隔列中提取具有完全匹配的行

时间:2017-02-28 08:30:01

标签: r dplyr subset

我的数据框看起来像这样。

UID    Words
 1     playbook, gold, fun, toxic
 2     play, silver, golden
 3     played, toxicwaste, funny, golden

我需要一个根据完全匹配过滤行的函数。即。如果我想提取含金的行,结果将是

UID   Words
 1    playbook, gold, fun, toxic

但是如果我想要带有黄金的行,输出应该是

 UID  Words
  2    play, silver, golden
  3    played, toxicwaste, funny, golden

1 个答案:

答案 0 :(得分:0)

假设您的数据框为df。我们可以使用grep列上的Words来提取与给定字匹配的行。

getMatchingRows <- function(x) {
   df[grep(paste0("\\b", x, "\\b"), df$Words),]
}


getMatchingRows("gold")
#  UID                   Words
#1   1 playbook,gold,fun,toxic

getMatchingRows("golden")
#  UID                          Words
#2   2             play,silver,golden
#3   3 played,toxicwaste,funny,golden

getMatchingRows("play")
#  UID              Words
#2   2 play,silver,golden