[R-es] wordcloud y tabla de palabras

Alfredo David Alvarado Ríos david.alvarado.r en gmail.com
Vie Jul 25 06:46:18 CEST 2014


Buenas noches grupo. Saludos cordiales.

He seguido en la búsqueda de una forma que me permita realizar la
comparación de dos documentos pertenecientes a los años 2005 y 2013, y
que pueda representar finalmente con wordcloud y con una table en la
que las columnas sean los años de cada informe "2005" y "2013", y las
filas sean las palabras con la frecuencia de cada una de ellas por
cada informe:


----------------------------------
|                |  2005 | 2013  |
----------------------------------
| terminos |           |           |
----------------------------------
| terminos |           |           |
----------------------------------


De manera que buscando y experimentando, adaptando de otras
experiencias logré llegar a lo siguiente:

##########

>informes<-c("2013", "2005")
>pathname<-"C:/Users/d_2/Documents/Comision/PLAN de INSPECCIONES/Informes/"

>TDM<-function(informes, pathname) {
 info.dir<-sprintf("%s/%s", pathname, informes)
 info.cor<-Corpus(DirSource(directory=info.dir, encoding="UTF-8"))
 info.cor.cl<-tm_map(info.cor, content_transformer(tolower))
 info.cor.cl<-tm_map(info.cor.cl, stripWhitespace)
 info.cor.cl<-tm_map(info.cor.cl,removePunctuation)
 sw<-readLines("C:/Users/d_2/Documents/StopWords.txt", encoding="UTF-8")
 sw<-iconv(enc2utf8(sw), sub = "byte")
 info.cor.cl<-tm_map(info.cor.cl, removeWords, stopwords("spanish"))
 info.tdm<-TermDocumentMatrix(info.cor.cl)
 result<-list(name = informes, tdm= info.tdm)
 }
>tdm<-lapply(informes, TDM, path = pathname)

Resultado:

> tdm
[[1]]
[[1]]$name
[1] "2013"
[[1]]$tdm
<<TermDocumentMatrix (terms: 1540, documents: 1)>>
Non-/sparse entries: 1540/0
Sparsity           : 0%
Maximal term length: 18
Weighting          : term frequency (tf)

[[2]]
[[2]]$name
[1] "2005"
[[2]]$tdm
<<TermDocumentMatrix (terms: 1849, documents: 1)>>
Non-/sparse entries: 1849/0
Sparsity           : 0%
Maximal term length: 19
Weighting          : term frequency (tf)

> str(tdm)
List of 2
 $ :List of 2
  ..$ name:  "2013"
  ..$ tdm :List of 6
  .. ..$ i       : int [1:1540] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ j       : int [1:1540] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ v       : num [1:1540] 1 5 8 1 1 1 1 3 1 1 ...
  .. ..$ nrow    : int 1540
  .. ..$ ncol    : int 1
  .. ..$ dimnames:List of 2
  .. .. ..$ Terms:  [1:1540] "070812" "100" "100000" "117735" ...
  .. .. ..$ Docs :  "2013_21SeguridadCiudadana.txt"
  .. ..- attr(*, "class")=  [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
  .. ..- attr(*, "weighting")=  [1:2] "term frequency" "tf"
 $ :List of 2
  ..$ name:  "2005"
  ..$ tdm :List of 6
  .. ..$ i       : int [1:1849] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ j       : int [1:1849] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ v       : num [1:1849] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ nrow    : int 1849
  .. ..$ ncol    : int 1
  .. ..$ dimnames:List of 2
  .. .. ..$ Terms:  [1:1849] "071004" "100000" "101005" "1022" ...
  .. .. ..$ Docs :  "05_seguridad_ciudadana.txt"
  .. ..- attr(*, "class")=  [1:2] "TermDocumentMatrix" "simple_triplet_matrix"
  .. ..- attr(*, "weighting")=  [1:2] "term frequency" "tf"
>
>m<-as.matrix(tdm)

########

Al probar str(tdm), se obtiene lo siguiente:

List of 2
 $ :List of 2
  ..$ name: chr "2013"
  ..$ tdm :List of 6
  .. ..$ i       : int [1:1540] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ j       : int [1:1540] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ v       : num [1:1540] 1 5 8 1 1 1 1 3 1 1 ...
  .. ..$ nrow    : int 1540
  .. ..$ ncol    : int 1
  .. ..$ dimnames:List of 2
  .. .. ..$ Terms: chr [1:1540] "070812" "100" "100000" "117735" ...
  .. .. ..$ Docs : chr "2013_21SeguridadCiudadana.txt"
  .. ..- attr(*, "class")= chr [1:2] "TermDocumentMatrix"
"simple_triplet_matrix"
  .. ..- attr(*, "weighting")= chr [1:2] "term frequency" "tf"
 $ :List of 2
  ..$ name: chr "2005"
  ..$ tdm :List of 6
  .. ..$ i       : int [1:1849] 1 2 3 4 5 6 7 8 9 10 ...
  .. ..$ j       : int [1:1849] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ v       : num [1:1849] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ nrow    : int 1849
  .. ..$ ncol    : int 1
  .. ..$ dimnames:List of 2
  .. .. ..$ Terms: chr [1:1849] "071004" "100000" "101005" "1022" ...
  .. .. ..$ Docs : chr "05_seguridad_ciudadana.txt"
  .. ..- attr(*, "class")= chr [1:2] "TermDocumentMatrix"
"simple_triplet_matrix"
  .. ..- attr(*, "weighting")= chr [1:2] "term frequency" "tf"
 - attr(*, "dim")= int [1:2] 2 1


> m<-as.matrix(tdm)

> m
     [,1]
[1,] List,2
[2,] List,2


 #######

 Pero no logro poder avanzar en los pasos para tener la comparativa de
palabras por años par avanzar en una visualización de wordcloud.
Si alguien se le ocurre alguna idea, agradecería mucho la ayuda.



Más información sobre la lista de distribución R-help-es