我有一个数据框,其中的列包含一些列表元素。我想找出数据框的哪些行包含该列中的关键字。
数据框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 [which(is.element(' wl',df $ tag)),]
但这仅返回数据框的第一个元素(无论它是否包含' wl')。我认为问题在于迭代行并实现" is.element"功能。以下是该函数的两个实现及其结果:
is.element('wl',df$tag[[4]]) > TRUE
is.element('wl',df$tag[4]) > FALSE
您如何建议我遍历数据框,为df_wl指定正确的值?
PS:这是输入:
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")
答案 0 :(得分:2)
根据您的dput
数据。这可能有用。正则表达式(^wl$)|(\"wl\")
从头到尾匹配wl
,或"wl"
的任何匹配(用双引号括起来)
df[grepl("(^wl$)|(\"wl\")", df$tag),]
# idstr tag
# 1 1 wl
# 4 4 c("wl","other.to")
# 5 5 wl