21.18 Creating a Document Term Matrix
A document term matrix is simply a matrix with documents as the rows and terms as the columns and a count of the frequency of words as the cells of the matrix. We use tm::DocumentTermMatrix() to create the matrix:
## <<DocumentTermMatrix (documents: 46, terms: 6508)>>
## Non-/sparse entries: 30059/269309
## Sparsity : 90%
## Maximal term length: 56
## Weighting : term frequency (tf)
We can inspect the document term matrix using tm::inspect(). Here, to avoid too much output, we select a subset of inspect.
## <<DocumentTermMatrix (documents: 5, terms: 6)>>
## Non-/sparse entries: 9/21
## Sparsity : 70%
## Maximal term length: 8
## Weighting : term frequency (tf)
## Sample :
## Terms
## Docs path patholog patient pbs per perhap
## acnn96.txt 0 0 0 0 0 0
## adm02.txt 0 0 0 0 0 0
## ai02.txt 4 6 9 4 4 1
## ai03.txt 0 0 11 11 0 0
## ai97.txt 0 0 3 0 0 0
The document term matrix is in fact quite sparse (that is, mostly empty) and so it is actually stored in a much more compact representation internally. We can still get the row and column counts.
## [1] "DocumentTermMatrix" "simple_triplet_matrix"
## [1] 46 6508
The transpose is created using tm::TermDocumentMatrix():
## <<TermDocumentMatrix (terms: 6508, documents: 46)>>
## Non-/sparse entries: 30059/269309
## Sparsity : 90%
## Maximal term length: 56
## Weighting : term frequency (tf)
We will use the document term matrix for the remainder of the chapter.
If you find this curated material useful then you can consider a donation to support it's ongoing availability and give you access to the PDF version of this book. The material has been scoped up by Generative AI without permission or any kind of recompense so do consider a donation if you can afford it. Unlike Generative AI your access to this materials is freely given. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Togaware has a 30 year tradition of making popular open source software which includes sold privacy preserving productivity apps, rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0