【发布时间】:2019-03-01 04:18:03
【问题描述】:
我有一个数据框(称为 all_data),如下所示:
Title Text
Title_1 Very interesting word_1 and also keyword_2
Title_2 hello keyword_1, and keyword_3.
我还有第二个数据框(称为关键字),如下所示:
keywords
word_1
word_2
word_3
word_4a word_4b word_4c
我想在 all_data 数据框中创建一个额外的列。在此列中,如果关键字之一(来自关键字数据框)出现在 all_data$Text 或 all_data$Title 列中,我想打印相关关键字。例如:
Title Text Keywords
Title_1 Very interesting word_1 and also word_2, word_1. word_1, word_2
Title_2 hello word_1, and word_3. word_1, word_3
Title_3 difficult! word_4b, and word_4a also word_4c word_4a word_4b word_4c
!只需在 all_data$Words 列中打印一次单词,而不是多次。 对我来说,更难的部分是打印一个“关键字”,例如:“keyword_A Keyword_A1 Keyword_A3”,只有当关键字的所有部分都出现在相关文本中时才会出现。
这里回答了这个问题(Recognize patterns in column, and add them to column in Data frame),我在这里使用了DJack他的解决方案:
ls <- strsplit(tolower(paste(all_data$Title, all_data$Text)),"(\\s+)|(?!')(?=[[:punct:]])", perl = TRUE)
all_data$Keywords <- do.call("rbind",lapply(ls,function(x) paste(unique(x[x %in% tolower(keywords)]), collapse = ", ")))
但是当多个关键字出现时它会失败(一个关键字,比如:老奶奶,如果你有这样的文字应该出现:“嘿,你的奶奶很好,而且很老”。
更新
@Nicolas2 帮我解决了问题(谢谢)。但不幸的是它失败了。任何人都知道如何解决这个问题?正如您在下面的示例中所看到的,例如,关键字“feyenoord skin”不应出现(因为“skin”没有出现在文本中)。我只希望关键字出现在文本中(或者有多个关键字,例如“Hello World”,如果所有单词都出现在文本中,那就太好了(所以 Hello and World)。非常感谢!
df <- data.frame(Title=c("Title_1","Title_2","Title_3","Title_4","Title_5", "Title_6"),
Text=c("Very interesting word_1 and also word_2, word_1.",
"hello word_1, and word_3.",
"difficult! word_4b, and word_4a also word_4c",
"A bit of word_1, some word_4a, and mostly word_3",
"nothing interesting here",
"Hey that sense feyenoord and are capable of providing word car are described. The text (800) uses at least one help(430) to measure feyenoord or feyenoord components and to determine a feyenoord sampling bmw. The word car is rstudio, at least in part, using the feyenoord sampling bmw. The feyenoord sampling bmw may be rstudio, at least in part, using a feyenoord volume (640) and/or a feyenoord generation bmw, both of which may be python or prerstudio."),
stringsAsFactors=F)
keywords<-data.frame(Keyword=c("word_1","word_2","word_3","word_4a word_4b word_4c",
"a feyenoord sense",
"feyenoord", "feyenoord feyenoord", "feyenoord skin", "feyenoord collection",
"skin feyenoord", "feyenoord collector", "feyenoord bmw",
"collection feyenoord", "concentration feyenoord", "feyenoord sample",
"feyenoord stimulation", "analyte feyenoord", "collect feyenoord",
"feyenoord collect", "pathway feyenoord feyenoord sandboxs",
"feyenoord bmw mouses", "sandbox", "bmw",
"pulse bmw three levels"),stringsAsFactors=F)
# split the keywords into words, but remember keyword length
k <- keywords %>% mutate(l=str_split(Keyword," ")) %>% unnest %>%
group_by(Keyword) %>% mutate(n=n()) %>% ungroup
# split the title into words
# compare with words from keywords
# keep only possibly multiple, but full matches
# collate all results and merge back to the original data
test <- df %>% mutate(l=str_split(Text,"[ .,]")) %>% unnest %>%
inner_join(k,by="l") %>%
group_by(Title,Keyword) %>% filter(n()%%n==0) %>%
distinct(Keyword) %>% ungroup %>% nest(Keyword) %>%
rowwise %>% mutate(keywords=paste(data[[1]],collapse=", ")) %>% select(-data) %>%
inner_join(df,.,by="Title")
View(test)
【问题讨论】:
-
我很确定我以前回答过一个与此完全相同的问题。更努力地尝试搜索功能!
-
无法在您个人资料的“答案”中找到它。在常规搜索栏中也找不到。