大期文档矩阵/ simple_triplet_matrix的行和?{tm package}

Ray*_*Ray 17 r text-mining

所以我有一个非常大的术语文档矩阵:

> class(ph.DTM)
[1] "TermDocumentMatrix"    "simple_triplet_matrix"

> ph.DTM
A term-document matrix (109996 terms, 262811 documents)

Non-/sparse entries: 3705693/28904453063
Sparsity           : 100%
Maximal term length: 191 
Weighting          : term frequency (tf)
Run Code Online (Sandbox Code Playgroud)

如何获得每个术语的rowSum(频率)?我试过了:

> apply(ph.DTM, 1, sum)
Error in vector(typeof(x$v), nr * nc) : vector size cannot be NA
In addition: Warning message:
In nr * nc : NAs produced by integer overflow
Run Code Online (Sandbox Code Playgroud)

显然,我知道removeSparseTerms:

ph.DTM2 <- removeSparseTerms(ph.DTM, 0.99999)
Run Code Online (Sandbox Code Playgroud)

这减少了一点:

> ph.DTM2
A term-document matrix (28842 terms, 262811 documents)

Non-/sparse entries: 3612620/7576382242
Sparsity           : 100%
Maximal term length: 24 
Weighting          : term frequency (tf)
Run Code Online (Sandbox Code Playgroud)

但我仍然无法应用任何与矩阵相关的函数:

> as.matrix(ph.DTM2)
Error in vector(typeof(x$v), nr * nc) : vector size cannot be NA
In addition: Warning message:
In nr * nc : NAs produced by integer overflow
Run Code Online (Sandbox Code Playgroud)

我怎样才能在这个对象上得到一个简单的行和?谢谢!!

Ray*_*Ray 22

好的,经过一些更多的谷歌,我遇到了slam包,这使得:

ph.DTM3 <- rollup(ph.DTM, 2, na.rm=TRUE, FUN = sum)
Run Code Online (Sandbox Code Playgroud)

哪个有效.

  • 四处搜寻,我找到了slam的`row_sums`功能,看起来有点快. (7认同)

chr*_*ell 11

正如@badpanda在其中一条评论中提到的,slam现在有稀疏数组的函数row_sumscol_sums函数:

slam::row_sums(dtm, na.rm = T)
slam::col_sums(tdm, na.rm = T)
Run Code Online (Sandbox Code Playgroud)