Skip to contents

This function takes a data frame and a specified variable name and divides the variable into ordered categories based on quantile breaks. The number of quantile divisions must be specified. The function is designed to handle non-unique breaks by adjusting them appropriately. It returns the data frame with a new ordered factor variable.

Usage

create_ordered_variable(df, var_name, n_divisions = NULL)

Arguments

df

A data frame containing the variable to be divided into quantiles.

var_name

The name of the variable within the data frame to divide into quantiles. For example, in the `df_nz` dataset, you might use "perfectionism".

n_divisions

The number of quantile divisions to create. This must be a positive integer. If NULL or not specified, the function will stop and ask the user to provide this parameter.

Value

The input data frame with an additional column representing the ordered factor variable. This new column is named by combining the original variable name, the number of divisions, and the suffix 'tile', e.g., 'perfectionism_5tile' for 5 divisions of the 'perfectionism' variable.

Examples

# Assuming df_nz is your dataset and 'perfectionism' is the column of interest:
df_updated <- create_ordered_variable(df_nz, "perfectionism", 5)