2012-07-16 16 views
7

Tengo un termDocumentMatrix creado usando el paquete tm en R.R crear la matriz de Nmost términos frecuentes

Estoy tratando de crear una matriz/trama de datos que tiene los 50 términos que aparecen con mayor frecuencia.

Cuando intento de convertir a una matriz me sale este error:

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

así que he intentado convertir a matrices dispersas utilizando el paquete de 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 

He intentado acceder a las diferentes partes del MDT usando:

> 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 

Aquí hay alguna otra información:

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

Mi salida ideal es algo como esto:

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

¿Alguna sugerencia?

Respuesta

9

Las matrices de documento de término en tm ya se han creado como matrices dispersas. Aquí, mydata.tdm$i y mydata.tdm$j son los vectores de índices de la matriz y mydata.tdm$v es el vector relacionado de frecuencias. De manera que se puede crear una matriz dispersa por escrito:

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

continuación, puede utilizar rowSums y enlazar las filas, que le interesa, a los términos, que representan, con $Terms.

+0

HTH: term.freq <- rowSums (as.matrix (TDM)) term.freq <- subconjunto (term.freq, term.freq> = 50) tdm_matrix <- as.matrix (TDM) tfMatrix <- tdm_matrix [names (term.freq)] – aloplop85

Cuestiones relacionadas