我有一个使用 TM 包创建的语料库,我已在其中应用了所有转换,并准备将其转换回数据框架。
当我使用时
twit[[1]]$content
我可以看到我的数据。但是,当我尝试取消列出它时,我的所有记录都为 NA。
twitCln <- data.frame(text=unlist(sapply(twit, '[', "content")), stringsAsFactors=F)
链接的问题Loop through a tm corpus without losing corpus structure在具有相同问题的唯一答案之后进行了讨论,但似乎没有解决方案。
这是一些可重现的代码。
library(tm)
bbTwit <- as.data.frame(c("Text Line One!", "Text Line 2"), stringsAsFactors = F)
colnames(bbTwit) <- 'Contents'
bbTwit$doc_id <- row.names(bbTwit)
twit <- bbTwit[c('doc_id','Contents')]
colnames(twit) <- c('doc_id','text')
twit <-Corpus(DataframeSource(data.frame(twit)))
twit <-tm_map(twit, removePunctuation)
twit <-tm_map(twit, stripWhitespace)
twit[[1]]$content
twitCln <- data.frame(text=unlist(sapply(twit, '[', "content")), stringsAsFactors=F)
预期输出将是一个包含 2 个观察值的数据框,其中“文本行 1”将是第一个记录,“文本行 2”将是第二个记录。我得到的是 NA 的两个观察结果
最佳答案
要获取内容,只需使用 content()
函数。例如
content(twit)
# [1] "Text Line One" "Text Line 2"
或者将其放入 data.frame
data.frame(text=content(twit))
# text
# 1 Text Line One
# 2 Text Line 2
关于r - 从 TM 包中取消列出 Corpus 即可获得 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50396427/