假设我有一个文档,其中包含一些文本,如以下内容:
doc <- 'Questions with similar titles have frequently been downvoted and/or closed. Consider using a title that more accurately describes your question.'
然后我可以创建一个数据帧,其中每个单词在df中都有一行:
library(stringi)
dfall <- data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc))))
我们将添加具有唯一ID的第三列。若要获取ID,请删除重复项:
library(dplyr)
uniquedf <- distinct(data.frame(words = unlist(stri_extract_all_words(stri_trans_tolower(doc)))))
我正在努力研究如何将行与两个数据帧进行匹配,以从中提取行索引值。
uniquedf
作为的新行值
df
alldf <- alldf %>% mutate(id = which(uniquedf$words == words))
像这样的dply方法不起作用。
有更有效的方法吗?
为了给出一个更简单的示例来显示预期的输出,我想要一个如下所示的数据帧:
words id
1 to 1
2 row 2
3 zip 3
4 zip 3
我的起始词vector是:
doc <- c('to', 'row', 'zip', 'zip')
或
doc <- c('to row zip zip')
. ID列为每个唯一的单词添加一个唯一的ID。