factorize_df: Convert all character vectors containing a set of values in a...

View source: R/factorize.R

factorize_dfR Documentation

Convert all character vectors containing a set of values in a data.frame to factors.

Description

This function examines each column in a data.frame; when it finds a column composed solely of the values provided to the lvls argument it updates them to be factor variables, with levels in the order provided.

This is an alternative to calling dplyr::mutate_at with factor() and identifying the specific variables you want to transform, if you have several repeated sets of responses.

Usage

factorize_df(dat, lvls, ignore.case = NULL)

Arguments

dat

data.frame with some factor variables stored as characters.

lvls

The factor levels in your variable(s), in order. If you have a question whose possible responses are a subset of another question's, don't use this function; manipulate the specific columns with dplyr::mutate_at.

ignore.case

Logical. If TRUE, will match without checking case, using the capitalization from the lvls parameter for the final output. If not provided, the function will provide a warning if it detects columns that would match without checking case but will NOT coerce them.

Value

a data.frame the same size as dat, with factorization completed in place.

Examples

teacher_survey |>
  factorize_df(lvls = c("Strongly Disagree", "Disagree", "Somewhat Disagree",
                        "Somewhat Agree", "Agree", "Strongly Agree"))

# prints warning due to case mismatches:
teacher_survey |>
  factorize_df(lvls = c("Strongly disagree", "Disagree", "Somewhat disagree",
                        "Somewhat agree", "Agree", "Strongly agree"))

tntp/tntpr documentation built on March 27, 2024, 6:26 p.m.