首先,让我们重新创建您的数据框,不要将字符值转换为因子:
x10 = data.frame(id = c(1,2,3), vars = c('top','down','top'),
text1 = c('this is text', 'so is this', 'and this is too.'),
text2 = c('we have more text here', 'and here too', 'and look at this, more text.'),
stringsAsFactors = FALSE)
接下来我们有两个选项。
方法一:将数据重塑为“长”格式并创建单个语料库
首先“融化”数据,使其成为一个列,然后作为语料库导入。(另一种选择是使用tidy::gather()
)。
x10b <- reshape2::melt(x10, id.vars = c("id", "vars"),
measure.vars = c("text1", "text2"),
variable.name = "doc_id", value.name = "text")
row.names(x10b) <- paste(x10b$doc_id, x10b$id, sep = "_")
x10b
x10_corpus <- corpus(x10b)
summary(x10_corpus)
方法二:制作两个语料库对象并组合
在这里,我们分别创建两个语料库对象,并使用+
运算符将它们组合起来。
x10_corpus2 <-
corpus(x10[, -which(names(x10)=="text2")], text_field = "text1") +
corpus(x10[, -which(names(x10)=="text1")], text_field = "text2")
summary(x10_corpus2)
你可以在这个阶段使用
docnames(x10_corpus2) <-
来重新分配文件名,使其更像第一种方法。