【发布时间】:2017-12-13 12:56:26
【问题描述】:
我一直在尝试编写一个函数或使用 apply 系列来选择数据框中包含我要查找的单词的行并将它们标记为标签。一行可以有多个标签。有人可以帮助我吗,我已经卡了一段时间了。
如果我的问题不清楚,或者在其他地方有答案,请引导我朝着正确的方向前进。非常感激!
require(stringr)
require(dplyr)
df <- data.frame(sentences, rnorm(length(sentences)))
old = df %>% filter(str_detect(sentences, 'old')) %>% mutate(w = factor("old"))
new = df %>% filter(str_detect(sentences, 'new')) %>% mutate(w = factor("new"))
boy = df %>% filter(str_detect(sentences, 'boy')) %>% mutate(w = factor("boy"))
girl = df %>% filter(str_detect(sentences, 'girl')) %>% mutate(w = factor("girl"))
tags <- bind_rows(old, new, boy, girl)
所以我想选择有限数量的单词,例如:
tags <- c('bananas', 'apples', oranges)
我希望结果是一个 data.frame,其中包含我选择的每个单词的新列。如果该行包含我选择的单词之一,则该单词的列应以某种方式标记为 TRUE och。类似的东西
Sentences bananas apples oranges
sentence1 TRUE
sentence2 TRUE
sentence3 TRUE
sentence4 TRUE
sentence5 TRUE TRUE
或
Sentences tag1 tag2
sentence1 bananas
sentence2 apples
sentence3 bananas
sentence4 oranges
entences5 apples oranges
或者类似的东西。如果我能解释得更清楚,请告诉我。
【问题讨论】:
-
您正在寻找的最终解决方案是什么?从概念上讲,它可以做什么?
-
您尝试标记的单词数量是否有限?
-
我试图解释更多,单词的数量是有限的,我希望每一行都被标记,如果它包含任何单词。我不知道什么可能是好的,要么是每个单词的列,要么标签 #1 #2 #3 最多(标签的 nr 个)。
-
你能提供
sentences吗?