R tm包创建了几乎常用术语的矩阵

scr*_*Owl 7 r text-mining tm term-document-matrix

我在R中termDocumentMatrix创建了一个tm包.

我正在尝试创建一个具有50个最常出现的术语的矩阵/数据帧.

当我尝试转换为矩阵时,我收到此错误:

> ap.m <- as.matrix(mydata.dtm)
Error: cannot allocate vector of size 2.0 Gb
Run Code Online (Sandbox Code Playgroud)

所以我尝试使用Matrix包转换为稀疏矩阵:

> A <- as(mydata.dtm, "sparseMatrix") 
Error in as(from, "CsparseMatrix") : 
  no method or default for coercing "TermDocumentMatrix" to "CsparseMatrix"
> B <- Matrix(mydata.dtm, sparse = TRUE)
Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法访问tdm的不同部分:

> freqy1 <- data.frame(term1 = findFreqTerms(mydata.dtm, lowfreq=165))
> mydata.dtm[mydata.dtm$ Terms %in% freqy1$term1,]
Error in seq_len(nr) : argument must be coercible to non-negative integer
Run Code Online (Sandbox Code Playgroud)

这是其他一些信息:

> str(mydata.dtm)
List of 6
 $ i       : int [1:430206] 377 468 725 3067 3906 4150 4393 5188 5793 6665 ...
 $ j       : int [1:430206] 1 1 1 1 1 1 1 1 1 1 ...
 $ v       : num [1:430206] 1 1 1 1 1 1 1 1 2 3 ...
 $ nrow    : int 15643
 $ ncol    : int 17207
 $ dimnames:List of 2
  ..$ Terms: chr [1:15643] "000" "0mm" "100" "1000" ...
  ..$ Docs : chr [1:17207] "1" "2" "3" "4" ...
 - attr(*, "class")= chr [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
 - attr(*, "Weighting")= chr [1:2] "term frequency" "tf"
> mydata.dtm
A term-document matrix (15643 terms, 17207 documents)

Non-/sparse entries: 430206/268738895
Sparsity           : 100%
Maximal term length: 54 
Weighting          : term frequency (tf)
Run Code Online (Sandbox Code Playgroud)

我的理想输出是这样的:

term      frequency
the         2123
and         2095
able         883
...          ...
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Pop*_*Pop 9

tm中的术语 - 文档矩阵已经创建为稀疏矩阵.这里,mydata.tdm$i并且mydata.tdm$j是矩阵的索引的向量,并且mydata.tdm$v是频率的相关向量.这样你就可以创建一个稀疏矩阵编写:

sparseMatrix(i=mydata.tdm$i, j=mydata.tdm$j, x=mydata.tdm$v)
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用rowSums和链接您感兴趣的行,它们代表的条款$Terms.