ohe: One-hot encodes a single data.frame column or vector

View source: R/ohe.R

oheR Documentation

One-hot encodes a single data.frame column or vector

Description

One-hot encodes a single data.frame column or vector

Usage

ohe(var, drop_ref = TRUE)

Arguments

var

A vector or data.frame column of categorical variables

drop_ref

Logical, default = TRUE. Will return x - 1 columns for x unique categories. Choose FALSE to return x columns.

Value

A data.frame of columns each representing a category in var with binary indicators 0/1.

Examples

# Encode a vector
lets <- rep(letters[1:3], 3)
ohe(lets)

# Encode a single column of a data.frame
df <- data.frame(lets, x = runif(9))
ohe(df$lets)

# Encode just the categorical columns and combine
df2 <- data.frame(lets, LETS = rep(LETTERS[1:3], 3), x = rnorm(9))
data.frame(lapply(df2, function(x) if(is.factor(x)) { ohe(x) } else { x } ))

gweissman/gmish documentation built on Feb. 21, 2025, 1:20 a.m.