2014-10-27 25 views
1

我有一個數據框,列中包含一些列表元素。我想知道哪一行數據框包含該列中的關鍵字。is.element數據框中的列表列

的數據幀,DF,看起來有點像這樣

idstr    tag 
1     wl 
2   other.to 
3   other.from 
4 c("wl","other.to") 
5     wl 
6   other.wl 
7 c("ll","other.to") 

的目標是在他們的標籤「WL」至一個新的數據幀分配的所有行。在這個例子中,我想,看起來像一個新的數據幀:

idstr tag 
1  wl 
4  c("wl","other.to") 
5  wl 

我想是這樣的

df_wl < - DF [其中(is.element( 'WL',DF $標籤)),]

但這隻返回數據幀的第一個元素(不論它是否包含'wl')。我認爲麻煩在於遍歷行並實現「is.element」函數。下面是功能的兩種實現方式,它的結果:

is.element('wl',df$tag[[4]]) > TRUE 
is.element('wl',df$tag[4]) > FALSE 

你怎麼建議我通過數據幀迭代來與它的正確賦值df_wl?

PS:這裏的dput:根據您的dput數據

structure(list(idstr = 1:7, tag = structure(c(6L, 5L, 4L, 2L, 6L, 3L, 1L), .Label =  c("c(\"ll\",\"other.to\")", "c(\"wl\",\"other.to\")", "other.wl", "other.from", "other.to", "wl"), class = "factor")), .Names = c("idstr", "tag"), row.names = c(NA, -7L), class = "data.frame") 
+1

什麼'DF [sapply(DF $標籤,函數(x)的任何(x ==「WL 「)),]' – 2014-10-27 19:58:15

+1

您是否嘗試過使用'grep'? – tcash21 2014-10-27 19:59:19

+0

謝謝理查德。它適用於這個小例子,但是當我將它應用於我的主數據集時,它爲每個元素返回了一個數據框,其中包含「NA」值。我認爲'any(x ==「wl」)'工作,因爲新的數據框看起來像合適的大小,所以現在可能是返回數據的問題 – zebrainatree 2014-10-27 20:15:05

回答

2

。這可能工作。正則表達式匹配(^wl$)|(\"wl\")wl開始到結束,或"wl"任何發生(雙引號括起來)

df[grepl("(^wl$)|(\"wl\")", df$tag),] 
# idstr    tag 
# 1  1     wl 
# 4  4 c("wl","other.to") 
# 5  5     wl 
+1

爲什麼不''df [grepl(「^ wl $ |'wl'」,df $ tag),]'?正則表達式的第一部分本身是「wl」,第二部分用單引號查找「wl」。 – 2014-10-27 20:19:49

+0

SO CLOSE!我想我沒有提供所有的邊緣案例。一些列表不能包含像'c(「ll」,「other.from」)的wl。這是另一個dput:結構(列表(idstr = 1:7,tag = structure(c(6L,5L,4L,2L, 6L,3L,1L),.Label = c(「c(\」ll \「, \「other.to \」)「,」c(\「wl \」,\「other.to \」)「, 」ll「,」other.from「,」other.to「,」wl「) ,class =「factor」)),.Names = c(「idstr」, 「tag」),row.names = c(NA,-7L),class =「data.frame」) – zebrainatree 2014-10-27 20:20:03

+0

@BrianDiggs - I was實際上即將發佈確切的正則表達式 – 2014-10-27 20:21:22