factorize_df {tntpr} | R 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 |
ignore.case |
Logical. If TRUE, will match without checking case, using the capitalization from the |
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"))