首页 > 解决方案 > R tidytext 包函数 unnest_tokens 的问题

问题描述

我想使用 tidytext 包中的函数 unnest_tokens 进行二元分析。它在几周前工作,但现在我的输出只是 NA。

我有以下 df str(df)

'data.frame':36199 obs。1 个变量:$ 单词:chr "jed" "tag" "neu" "verunsichern" ...

当我使用以下代码进行二元分析时,bigrams <- df %>% unnest_tokens_(bigram, words, token = "ngrams", n = 2)我的输出只有 NA:

head(bigrams): | | 二元组 | | -------- | -------------- | | 1.内容| 不适用 | | 2.内容| 不适用 |

我需要先使用 tm_map 函数清理加载的文本,然后按如下方式创建 df: df0 <- data.frame(text=unlist(sapply(myVCorpus, [, "content")), stringsAsFactors=F)df<- df0 %>% unnest_tokens(words, text). 我更新了最新的 R 并更新了我的包。我真的不知道,为什么它停止工作。

预先感谢您的意见。亲切的问候克里斯托夫

更新:它必须与准备好的df有关。如果我使用 df,在我清理它之前整个句子都在一个单元格中,它可以工作。这就是我清理文本的过程: df<- %>% unnest_tokens(word, text) myVCorpus <- VCorpus(VectorSource(df)) myVCorpus <- tm_map(text.prep, removeWords, stopwords(kind= "de"))

更新2:我找到了解决方案。我需要整理准备好的df,这篇文章有帮助:unnest_tokens的对面,代码如下text.ngram<-df%>% group_by(mygroupingvariable) %>% summarize(text = str_c(words, collapse = " ")) %>% ungroup()

标签: runnesttidytext

解决方案


推荐阅读