Skip to contents

Total count of each vocabulary term over the documents of an STM input (documents is a list of 2-row matrices: term indices, counts), optionally within the groups defined by covariates, e.g. the most frequent terms by decade or by document type.

Usage

term_counts(stm_data, by = NULL, ...)

Arguments

stm_data

A list with documents and vocab (e.g. the output of tokens_to_stm(), stm::prepDocuments() or quanteda::convert(to = "stm")).

by

Optional character vector of columns of stm_data$meta (one row per document, as built by tokens_to_stm()) to count by. NULL counts over the whole corpus.

...

Not used: an argument renamed in the development version of mintR gives an error naming its replacement.

Value

A data.table with the by columns (if any), term and count, by decreasing count. Terms that never occur (in the group) are absent.

Examples

stm_data <- list(
  documents = list(matrix(c(1L, 2L, 2L, 1L), 2), matrix(c(2L, 3L), 2)),
  vocab = c("wage", "labour"),
  meta = data.frame(type = c("a", "b"))
)
term_counts(stm_data)
#>      term count
#>    <char> <num>
#> 1: labour     4
#> 2:   wage     2
term_counts(stm_data, by = "type")
#>      type   term count
#>    <char> <char> <num>
#> 1:      b labour     3
#> 2:      a   wage     2
#> 3:      a labour     1