Skip to contents

This function creates an ordered categorical variable in a dataframe based on user-specified breaks and labels. It checks that the provided breaks and labels are correctly specified before creating the factor variable. This is useful for creating a factor with custom defined intervals.

Usage

create_ordered_variable_custom(df, var_name, breaks, labels)

Arguments

df

A data frame containing the variable to be transformed.

var_name

The name of the numeric variable within the data frame to be converted into an ordered factor. This variable should ideally be continuous.

breaks

A numeric vector specifying the breakpoints between intervals. The breakpoints should cover the entire range of the variable and should be one more than the number of labels.

labels

A character vector specifying the labels for the intervals defined by breaks. The length of labels should be one less than the length of breaks.

Value

Returns the data frame with an additional column representing the ordered factor variable. The new column is named by combining the original variable name and the suffix "_coarsen".

Examples

# Assuming df_nz is your dataset and 'hours_exercise' is the numeric column:
df_updated <- create_ordered_variable_custom(df_nz, "hours_exercise",
                                             c(1, 2, 7, Inf), c("[1_2)", "[2_7)", "[7_up]"))