prep_tidy_text: Unnest tokens for each label in a labelled text

Description Usage Arguments Value Examples

View source: R/prep_tidy_text.R

Description

Creates table with tokens for each class (if any).

Usage

1
prep_tidy_text(x, target_col_name = NULL, text_col_name)

Arguments

x

x A data frame with one or more columns: the column with the classes (if target_col_name is not NULL); and the column with the text. Any other columns will be ignored.

target_col_name

A string with the column name of the target variable. Defaults to NULL.

text_col_name

A string with the column name of the text variable.

Value

A data frame with two or three columns: classes (if target_col_name is not NULL); line numbers; and tokens.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#' library(experienceAnalysis)
books <- janeaustenr::austen_books() # Jane Austen books
emma <- paste(books[books$book == "Emma", ], collapse = " ") # String with whole book
pp <- paste(books[books$book == "Pride & Prejudice", ], collapse = " ") # String with whole book

# Make data frame with books Emma and Pride & Prejudice
x <- data.frame(
  text = c(emma, pp),
  book = c("Emma", "Pride & Prejudice")
)

# Tokens for both books, without the `target_col_name` column
prep_tidy_text(x, target_col_name = NULL, text_col_name = "text") %>%
    head()

# Tokens for both books, with the `target_col_name` column this time
prep_tidy_text(x, target_col_name = "book", text_col_name = "text") %>%
    split(.$book) %>%
    purrr::map(~ head(.))

# Tokens for Pride & Prejudice
prep_tidy_text(x, target_col_name = "book", text_col_name = "text") %>%
    dplyr::filter(book == "Pride & Prejudice") %>%
    head()

CDU-data-science-team/experienceAnalysis documentation built on Dec. 17, 2021, 12:53 p.m.