dummy_to_categorical: Convert Dummy Variables to Categorical Variable

View source: R/dummy_to_categorical.R

dummy_to_categoricalR Documentation

Convert Dummy Variables to Categorical Variable

Description

This function converts a set of dummy (one-hot encoded) variables into a categorical variable. It assigns a category based on the column with the highest value in each row. If all values in a row are zero, it assigns a default value.

Usage

dummy_to_categorical(..., levels = NULL, default = NA)

Arguments

...

A set of logical or binary numeric vectors (or a data frame) representing dummy variables.

levels

A character vector specifying the category levels. Defaults to column names.

default

The value to assign when all dummy variables in a row are zero. Defaults to NA.

Value

A factor variable with levels corresponding to the input categories and the default value.

Examples

dummy_data <- data.frame(A = c(TRUE, FALSE, FALSE),
                         B = c(FALSE, TRUE, FALSE),
                         C = c(FALSE, FALSE, TRUE))
dummy_to_categorical(dummy_data)

dummy_data2 <- data.frame(A = c(1, 0, 0, 0),
                          B = c(0, 1, 0, 0),
                          C = c(0, 0, 1, 0))
dummy_to_categorical(dummy_data2, default = "None")


WhiteJP/jpw documentation built on April 17, 2025, 5:47 a.m.