1
votes

I want to convert a corpus to a DocumentTermMatrix with only selected words being tabulated. I know the "dictionary" parameter in the control list does this:

     a = list("I am a big big big apple", "Petter Petter Peter Peter")
     v = VCorpus(VectorSource(a))
     my_terms = c("peter", "petter")
     DocumentTermMatrix(v, control = list(dictionary = my_terms)) %>% as.matrix()

It gives me this:

        Terms
    Docs peter petter
       1     0      0
       2     1      1

Whereas what I want looks like this:

        Terms
    Docs peter petter
       1     0      0
       2     2      2
  1. The first document, though empty, must remain there. (Because it must be matched with a meta-data)
  2. The frequency of the word must be shown in the output.

I was wondering if there is a function/parameter does this.

1
Running your code (after installing and loading the tm package), gives me the second results (what you want). Please, check this again.KoenV
Is there a version problem here? Cause it definitely does not work for me...user7453767

1 Answers

0
votes

It works fine:

library(magrittr)
library(tm)

a <- list("I am a big big big apple", "Petter Petter Peter Peter")
v <- VCorpus(VectorSource(a))
my_terms <- c("peter", "petter")
DocumentTermMatrix(v, control = list(dictionary = my_terms)) %>% 
         as.matrix()