我的数据框看起来像这样。
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
答案 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