如何正确地将 .txt 文件加载到 R 中的 Vcor​​pus 中?

How to correctly Load .txt files into Vcorpus in R?

所有。 我想在 R 中分析几个 .txt 文件的内容。导入它们时遇到问题。 这是我的代码(data/txt/2012/ 目录中有 238 个 .txt 文件):

library(tm)   
cname <- file.path("../data", "txt", "2012")
docs <- Corpus(DirSource(cname), readerControl=list(reader=readPlain))

现在,如果我查看文档,它是一个包含 238 个文档的 Vcor​​pus,如预期的那样:

> docs
    <<VCorpus>>
    Metadata:  corpus specific: 0, document level (indexed): 0
    Content:  documents: 238

这里是我无法理解正在发生的事情的地方:

> docs[1]
    <<VCorpus>>
    Metadata:  corpus specific: 0, document level (indexed): 0
    Content:  documents: 1

> docs[[1]]
   <<PlainTextDocument>>
   Metadata:  7
   Content:  chars: 2156

在我看来,Vcorpus 有两个级别,第一个包含所有 238 个文档,第二个包含一个文档。我只想拥有一个包含 238 个文档的 Vcor​​pus,然后是 PlainTextDocument,预期输出将是(注意我只使用 [1] 而不是 [[1]] 来获取 PlainTextDocument):

> docs[1]
   <<PlainTextDocument>>
   Metadata:  7
   Content:  chars: 2156

有什么方法可以将 .txt 文件加载到具有所需格式的 Vcor​​pus 中吗? 或者我应该使用现在正在加载的方式?

非常感谢。 干杯。

在我看来你已经正确地加载了语料库。

tm 包的介绍文档说您可以使用,比方说,writeLines(as.character(docs[[4]])) 来获取文档 4 的文本表示。

您也可以使用content(docs[[4]])