Skip to contents

Add values from a dictionary

Usage

add_dictionary(df, dictionary, feature = word, keep_term = NULL)

Arguments

df

A tidy data frame, potentially containing a column called "word"

dictionary

A data frame with two or more columns, potentially made with make_dictionary()

feature

The column (like "word") to use for looking up values in the dictionary

keep_term

Whether to retain the original term value. This option is especially useful with dictionaries containing terms longer than one word in length; the NULL value will keep the term for these dictionaries while discarding it for those with terms of only one word.

Value

The original data frame with one or more columns added.

Examples

dubliners <- get_gutenberg_corpus(2814) |>
  load_texts() |>
  identify_by(part) |>
  standardize_titles()

my_sentiments <- make_dictionary(
  list(
    "😊" = c("happy", "joy", "smile"),
    "😔" = c("unhappy", "sad", "frown")),
  name = "emoji")

dubliners |>
   add_dictionary(my_sentiments) |>
   drop_na() |>
   head()
#> # A tibble: 6 × 6
#>   doc_id       title     author       part         word    emoji
#>   <fct>        <chr>     <chr>        <chr>        <chr>   <chr>
#> 1 The Sisters  Dubliners Joyce, James THE SISTERS  smile   😊   
#> 2 The Sisters  Dubliners Joyce, James THE SISTERS  smile   😊   
#> 3 An Encounter Dubliners Joyce, James AN ENCOUNTER happy   😊   
#> 4 Araby        Dubliners Joyce, James ARABY        smile   😊   
#> 5 Eveline      Dubliners Joyce, James EVELINE      happy   😊   
#> 6 Eveline      Dubliners Joyce, James EVELINE      unhappy 😔