replace_na: Replace Missing Values

View source: R/one_hot.R

replace_naR Documentation

Replace Missing Values

Description

Replace missing values with a specified value or mean/median value.

Usage

replace_na(dt, repl)

Arguments

dt

A data frame or vector.

repl

Replace missing values with a specified value such as -1, or the mean/median value for numeric variable and mode value for categorical variable if repl is mean or median.

Examples

# load germancredit data
data(germancredit)

library(data.table)
dat = rbind(
  setDT(germancredit)[, c(sample(20,3),21)],
  data.table(creditability=sample(c("good","bad"),10,replace=TRUE)),
  fill=TRUE)

## replace with -1
dat_repna1 = replace_na(dat, repl = -1)
## replace with median for numeric, and mode for categorical
dat_repna2 = replace_na(dat, repl = 'median')
## replace with mean for numeric, and mode for categorical
dat_repna3 = replace_na(dat, repl = 'mean')


scorecard documentation built on Aug. 8, 2023, 5:07 p.m.