我有一個數據框,列中包含一些列表元素。我想知道哪一行數據框包含該列中的關鍵字。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")
什麼'DF [sapply(DF $標籤,函數(x)的任何(x ==「WL 「)),]' – 2014-10-27 19:58:15
您是否嘗試過使用'grep'? – tcash21 2014-10-27 19:59:19
謝謝理查德。它適用於這個小例子,但是當我將它應用於我的主數據集時,它爲每個元素返回了一個數據框,其中包含「NA」值。我認爲'any(x ==「wl」)'工作,因爲新的數據框看起來像合適的大小,所以現在可能是返回數據的問題 – zebrainatree 2014-10-27 20:15:05