Thursday, 15 April 2010

matrix - How to build a termdocumentmatrix in R -


I was wondering if it is possible to create a term download matrix without using the package TM.

I was thinking about using two loops in combination with a grep, but unfortunately I did not manage to make some useful.

  Matrix & lt; - Matrix (, nrow = (in length (leverster), = ncol = length (levector)) (i in 1: length) (for in j: 1: length (l)) {lijst & lt; - [grep (lvector [i], l [J]] (length (lists) == 0) {matrix [i, j] == 0} and {matrix [i, j] == 1}}}  

thx in advance

FWW, here's a way:

  get.dtm & lt; - function (txts) {require (plyr) dtm & lt; - do.call (rbind.fill.matrix, lapply (txts, function (txt) t (table (Scan (file = text connection (txt), what = "character", quiet = True))))) dtm [is.na (dtm)] <0 - Return (dtm)} get.dtm (c ("This is a text lesson", "this is just another text")) # This is the second lesson # [1,] 1 1 2 1 0 0 # [2,] 0 1 1 1 1  

No comments:

Post a Comment