Learn R Programming

dentomedical (version 0.2.0)

impute_missing: Impute Missing Values in a Data Frame

Description

This function imputes missing values in a data frame. For categorical variables (factor or character), missing values are replaced with the mode. For numeric variables, missing values can be imputed using the mean, median, or regression-based imputation. If no method is specified for numeric columns, missing values are left as NA.

Usage

impute_missing(data, method = NULL)

Value

A data frame with missing values imputed according to the specified method.

Arguments

data

A data frame containing numeric and/or categorical variables with missing values.

method

A character string specifying the imputation method for numeric columns. Options are "mean", "median", "regression", or NULL (default: NULL).

Examples

Run this code
library(dplyr)
# Impute numeric columns using regression and categorical with mode
impute_missing(starwars, method = "regression")

# Impute numeric columns using mean
impute_missing(starwars, method = "mean")

# Impute numeric columns using median
impute_missing(starwars, method = "median")

Run the code above in your browser using DataLab