Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

flatten_dictionary

Flatten a hierarchical dictionary into a list of character vectors


Description

Converts a hierarchical dictionary (a named list of named lists, ending in character vectors at the lowest level) into a flat list of character vectors. Works like unlist(dictionary, recursive = TRUE) except that the recursion does not go to the bottom level. Called by dfm().

Usage

flatten_dictionary(
  dict,
  levels = 1:100,
  level = 1,
  key_parent = "",
  dict_flat = list()
)

Arguments

dict

list to be flattened

levels

integer vector indicating levels in the dictionary

level

internal argument to pass current levels

key_parent

internal argument to pass for parent keys

dict_flat

internal argument to pass flattened dictionary

Value

A dictionary flattened to variable levels

Author(s)

Kohei Watanabe

Examples

dict1 <-
    dictionary(list(populism=c("elit*", "consensus*", "undemocratic*", "referend*",
                               "corrupt*", "propagand", "politici*", "*deceit*",
                               "*deceiv*", "*betray*", "shame*", "scandal*", "truth*",
                               "dishonest*", "establishm*", "ruling*")))
flatten_dictionary(dict1)

dict2 <- list(level1a = list(level1a1 = c("l1a11", "l1a12"),
                             level1a2 = c("l1a21", "l1a22")),
              level1b = list(level1b1 = c("l1b11", "l1b12"),
                             level1b2 = c("l1b21", "l1b22", "l1b23")),
              level1c = list(level1c1a = list(level1c1a1 = c("lowest1", "lowest2")),
                             level1c1b = list(level1c1b1 = c("lowestalone"))))
flatten_dictionary(dict2)
flatten_dictionary(dict2, 2)
flatten_dictionary(dict2, 1:2)

quanteda

Quantitative Analysis of Textual Data

v3.0.0
GPL-3
Authors
Kenneth Benoit [cre, aut, cph] (<https://orcid.org/0000-0002-0797-564X>), Kohei Watanabe [aut] (<https://orcid.org/0000-0001-6519-5265>), Haiyan Wang [aut] (<https://orcid.org/0000-0003-4992-4311>), Paul Nulty [aut] (<https://orcid.org/0000-0002-7214-4666>), Adam Obeng [aut] (<https://orcid.org/0000-0002-2906-4775>), Stefan Müller [aut] (<https://orcid.org/0000-0002-6315-4125>), Akitaka Matsuo [aut] (<https://orcid.org/0000-0002-3323-6330>), William Lowe [aut] (<https://orcid.org/0000-0002-1549-6163>), Christian Müller [ctb], European Research Council [fnd] (ERC-2011-StG 283794-QUANTESS)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.