7

Rのパッケージを使用してtermDocumentMatrix作成しました。tm

最も頻繁に発生する50の用語を持つマトリックス/データフレームを作成しようとしています。

行列に変換しようとすると、次のエラーが発生します。

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

そこで、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

以下を使用して、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

その他の情報は次のとおりです。

> 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)

私の理想的な出力は次のようなものです。

term      frequency
the         2123
and         2095
able         883
...          ...

助言がありますか?

4

1 に答える 1

9

tm の用語ドキュメント マトリックスは、スパース マトリックスとして既に作成されています。ここで、mydata.tdm$imydata.tdm$jは行列のインデックスのベクトルで、mydata.tdm$vは関連する周波数のベクトルです。スパース行列を作成できるように、次のように記述します。

sparseMatrix(i=mydata.tdm$i, j=mydata.tdm$j, x=mydata.tdm$v)

次にrowSums、興味のある行を使用して、用語を表す用語にリンクできます$Terms

于 2012-07-17T07:33:02.370 に答える