1

是否存在同时连接两个包含不同列数和行数的 dfm 矩阵的方法?它可以通过一些额外的编码来完成,所以我对临时代码不感兴趣,而是对通用且优雅的解决方案(如果有的话)感兴趣。

一个例子:

dfm1 <- dfm(c(doc1 = "This is one sample text sample."), verbose = FALSE)
dfm2 <- dfm(c(doc2 = "Surprise! This is one sample text sample."), verbose = FALSE)
rbind(dfm1, dfm2)

给出一个错误。

'tm' 包可以直接连接它的 dfm 矩阵;它对我的目的来说太慢了。

还记得来自“quanteda”的“dfm”是一个 S4 类。

4

1 回答 1

4

如果您使用的是最新版本,则应该“开箱即用”:

packageVersion("quanteda")
## [1] ‘0.9.6.9’

dfm1 <- dfm(c(doc1 = "This is one sample text sample."), verbose = FALSE)
dfm2 <- dfm(c(doc2 = "Surprise! This is one sample text sample."), verbose = FALSE)

rbind(dfm1, dfm2)
## Document-feature matrix of: 2 documents, 6 features.
## 2 x 6 sparse Matrix of class "dfmSparse"
##      is one sample surprise text this
## doc1  1   1      2        0    1    1
## doc2  1   1      2        1    1    1

另请参阅dfm 对象?selectFeatures在哪里features(帮助文件中有示例)。

补充

请注意,这将正确对齐公共特征集中的两个文本,这与矩阵的常规rbind方法不同,矩阵的列必须匹配。出于同样的原因,对于具有不同术语的 DocumentTermMatrix 对象,实际上在tm包中rbind()不起作用:

require(tm)
dtm1 <- DocumentTermMatrix(Corpus(VectorSource(c(doc1 = "This is one sample text sample."))))
dtm2 <- DocumentTermMatrix(Corpus(VectorSource(c(doc2 = "Surprise! This is one sample text sample."))))
rbind(dtm1, dtm2)
## Error in f(init, x[[i]]) : Numbers of columns of matrices must match.

这几乎得到了它,但似乎重复了重复的功能:

as.matrix(rbind(c(dtm1, dtm2)))
##     Terms
## Docs one sample sample. text this surprise!
##    1   1      1       1    1    1         0
##    1   1      1       1    1    1         1
于 2016-05-22T03:39:32.737 回答