2017-02-28 37 views
0

我有一個看起來像這樣的數據框。在逗號分隔列中提取完全匹配的行

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

好像那種數據集中的地方可能會更好,以保持數據在相當長的形式不是這樣的形式.... – A5C1D2H2I1M1N2O1R2T1

+0

的可能的複製http://stackoverflow.com/questions/26813667/如何使用grep查找精確匹配 – akrun

+2

可能的重複[如何使用grep()查找完全匹配](http://stackoverflow.com/questions/26813667/how-to-use- grep的找到的,精確匹配) – m0nhawk

回答

0

假設你的數據幀作爲df。我們可以在Words列上使用grep來提取與給定詞匹配的行。

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