one_hot: One-Hot Encoding for Categorical Data

View source: R/functions.R

one_hotR Documentation

One-Hot Encoding for Categorical Data

Description

Converts raw categorical string/factor data matrix into numeric one-hot encoded data matrix. Intended to help prepare data to be passed to kbal argument allx when categorical data is used.

Usage

one_hot(data)

Arguments

data

a dataframe or matrix where columns are string or factor type covariates

Value

onehot_data

a matrix of combined sample and population data with rows corresponding to units and columns one-hot encoded categorical covariates

Examples


#Ex 1. Make up some categorical demographic data
dat = data.frame(pid = c(rep("Rep", 20),
                         rep("Dem", 20), 
                         rep("Ind", 20)), 
                 gender = c(rep("female", 35),
                            rep("male", 25)))
#Convert to one-hot encoded data matrix:
onehot_dat = one_hot(dat)

#Ex 2. lalonde data
data(lalonde)
cat_vars=c("black","hisp","married","nodegr","u74","u75")
onehot_lalonde = one_hot(lalonde[, cat_vars])

chadhazlett/KBAL documentation built on Jan. 3, 2024, 9:57 p.m.