View source: R/dummy_to_categorical.R
dummy_to_categorical | R Documentation |
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.
dummy_to_categorical(..., levels = NULL, default = NA)
... |
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 |
A factor variable with levels corresponding to the input categories and the default value.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.