R: Convert a 'DocumentTermMatrix'/'TermDocumentMatrix' into...

tidy_colo_tdm {textshape} | R Documentation

## Convert a `DocumentTermMatrix`/`TermDocumentMatrix` into Collocating Words in Tidy Form

### Description

Converts non-zero elements of a `DocumentTermMatrix`/`TermDocumentMatrix` into a tidy data set made of collocating words.

### Usage

    tidy_colo_tdm(x, ...)

    tidy_colo_dtm(x, ...)

### Arguments

`x` | A `DocumentTermMatrix`/`TermDocumentMatrix`.
`...` | Ignored.

### Value

Returns a tidied data.frame.

### See Also

`unique_pairs`

### Examples

    data(simple_dtm)

    tidied <- tidy_colo_dtm(simple_dtm)
    tidied
    unique_pairs(tidied)

    ## Not run:
    if (!require("pacman")) install.packages("pacman")
    pacman::p_load_current_gh('trinker/gofastr', 'trinker/lexicon')
    pacman::p_load(tidyverse, magrittr, ggstance)

    my_dtm <- with(
        presidential_debates_2012,
        q_dtm(dialogue, paste(time, tot, sep = "_"))
    )

    tidy_colo_dtm(my_dtm) %>%
        tbl_df() %>%
        filter(!term_1 %in% c('i', lexicon::sw_onix) &
            !term_2 %in% lexicon::sw_onix
        ) %>%
        filter(term_1 != term_2) %>%
        unique_pairs() %>%
        filter(n > 15) %>%
        complete(term_1, term_2, fill = list(n = 0)) %>%
        ggplot(aes(x = term_1, y = term_2, fill = n)) +
            geom_tile() +
            scale_fill_gradient(low= 'white', high = 'red') +
            theme(axis.text.x = element_text(angle = 45, hjust = 1))

    ## End(Not run)

[Package textshape version 1.6.0 Index]