Skip to contents

Factorize a column. This function is typically not called directly, but is called as part of the factorize_df function.

Usage

factorize_column(
  x,
  var_str,
  vals_df,
  variable_colname = "variable",
  value_colname = "value",
  value_label_colname = "value_label",
  value_order_colname = "value",
  extra_labels = NULL,
  add_na = TRUE
)

Arguments

x

A vector (or variable) to be 'factorized' or labeled. X is often a single variable or column within a dataframe.

var_str

The name of the vector or variable to be factorized (e.g., "sample_segment").

vals_df

A dataframe of variable labels (i.e., factor levels and labels) with the format as specified below.

variable_colname

The name of the variable column in vals_df. Default is 'variable'.

value_colname

The name of the value column in vals_df. Default is 'value',

value_label_colname

The name of the value label column in vals_df. Default is 'value_label'

value_order_colname

The name of the value order column in vals_df (can be the value column itself). Default is 'value'.

extra_labels

Pass a vector of the names of other variables in the values dataframe to use when labeling. Common uses include missing values, universal values (e.g., "Total" or "Subtotal" row), or similar variables (e.g., "mode_1" to reuse the mode_1 labels for mode_2). Default is NULL.

add_na

TRUE or FALSE setting for whether to add NA as an explicit level in the factor. Default setting of TRUE.

Value

A 'factorized' version of the vector it was passed (i.e., a factor). This is returned invisibly.

Details

The function expects a values dataframe (vals_df) with columns for the variable name, the value, the value label, and the value order (can be the same as the value itself)

The "factorize" functions were borrowed and updated from the 'tmr.Rite.out.tester' package by Matt Landis.

Examples


factored = factorize_column(
  x = hh$income_detailed,
  var_str = "income_detailed",
  vals_df = value_labels,
  extra_labels = "Missing",
  value_label_colname = "label"
)