将语料库转换为 Dataframe 返回NA

jchrr9hc  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(167)

我试图将我的语料库转换回 Dataframe ,但它只返回NA的。请帮助
代码:

library(wordcloud)
df <- data.frame(Description = c("I like this service very much"," this is worth it so much"))

corpus <- Corpus(VectorSource(df$Description))

corpus <- tm_map(corpus, tolower)

corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeWords, c(stopwords('english')))
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, stemDocument)

#a <- as.data.frame(corpus)

dataframe<-data.frame(text=unlist(sapply(corpus, `[`, "content")), 
                      stringsAsFactors=F)

预期输出:
Dataframe

'like servic much'
'worth much'
xoefb8l8

xoefb8l81#

这下面的代码为我工作:

dataframe <- data.frame(text = sapply(corpus, as.character), stringsAsFactors = FALSE)

输出:

text
1 like servic much
2       worth much
acruukt9

acruukt92#

下面的代码对我也有效。dataframe〈- data.frame(text = sapply(语料库,as.character),stringsAsFactors = FALSE)
输入:df〈- data.frame(Description = c(“我喜欢旅行,它扩展了我的视野,我参观了12个国家公园。”))
产出:如旅游拓展展望参观12个国家公园

相关问题