是否有一种方法可以同时连接包含不同列数和行数的两个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类。
发布于 2016-05-22 03:39:32
如果您使用的是最新版本,则应“开箱即用”:
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还请参见?selectFeatures,其中features是dfm对象(帮助文件中有示例)。
添加了
请注意,这将正确地对齐两个文本在一个共同的功能集中,不像通常的rbind方法的矩阵,其列必须匹配。出于同样的原因,rbind()实际上不适用于具有不同术语的tm包中的DocumentTermMatrix对象:
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 1https://stackoverflow.com/questions/37363711
复制相似问题