impute_missing_values: Impute missing values in a dataset

View source: R/impute.R

impute_missing_valuesR Documentation

Impute missing values in a dataset

Description

Replaces missing (NA) values in a dataset using either simple summary statistics or k-nearest-neighbours (kNN) imputation.

Usage

impute_missing_values(
  data,
  ...,
  method = "auto",
  constant = NULL,
  k = 5,
  ignore = NULL
)

Arguments

data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

...

<dplyr_tidy_select> Variables (columns) to impute. If none are supplied, all variables are imputed.

method

Imputation method. One of "auto" (default), "median", "mean", "mode", "constant", or "knn". See Details.

constant

Value used to replace missing values when method = "constant". Must be of the same type as the variables being imputed so that their class is preserved (a whole number for integer variables); an incompatible value is an error. For factors, a value which is not an existing level is added as one.

k

Number of nearest neighbours to use when method = "knn" (Default: 5).

ignore

<dplyr_tidy_select> Variables (columns) to exclude from being used as predictors when method = "knn" (e.g. a row identifier). Ignored for the simple methods.

Details

Two families of imputation are provided:

Simple imputation

Each selected variable (column) is imputed independently using a summary statistic of its own non-missing values.

  • "auto" (default): numeric variables are imputed with their median and non-numeric variables with their mode (most frequent value).

  • "median" / "mean": numeric variables are imputed with the chosen statistic. Non-numeric variables fall back to the mode (a message is emitted) as means and medians are undefined for them.

  • "mode": all selected variables are imputed with their mode.

  • "constant": all selected variables have missing values replaced with constant.

kNN imputation (method = "knn")

Missing values are imputed from the k most similar rows (samples), using all other variables as predictors. Similarity is measured with Gower distance, which handles mixed numeric and categorical data. This method uses kNN from the VIM package, which is only required (Suggests) when this method is used. Unlike the simple methods, imputed values depend on each row's own profile, better preserving relationships between variables.

Only variables (columns) selected via ... are imputed; if none are supplied all variables are eligible. For method = "knn", all variables (except any supplied to ignore) are still used to compute distances, even if they are not themselves imputed.

Imputation never changes the class of a variable:

  • Integer variables are imputed with a rounded integer, as the median or mean of a set of integers is frequently fractional. The rounding is reported via a message.

  • Factors gain the imputed value as an additional level if it is not already one.

  • Any other type mismatch (e.g. a character constant for a numeric variable) is an error rather than a silent conversion of the whole variable.

Variables which contain no non-missing values are skipped (there is no statistic to impute from) and reported as such, except under method = "constant" where constant does not depend on the observed values.

Imputation is deterministic: the same input always yields the same output. The number of values imputed per variable is reported via a message (silence with suppressMessages). Value-level changes can be reviewed with review_quality_ctrl.

Value

data with missing values in the selected variables imputed.

References

Kowarik, A. & Templ, M. Imputation with the R Package VIM. Journal of Statistical Software 74, 1-16 (2016). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v074.i07")}

Gower, J. C. A General Coefficient of Similarity and Some of Its Properties. Biometrics 27, 857-871 (1971). \Sexpr[results=rd]{tools:::Rd_expr_doi("10.2307/2528823")}

Little, R. J. A. & Rubin, D. B. Statistical Analysis with Missing Data. (John Wiley & Sons, 2019).

See Also

Other high level functionality: apply_quality_ctrl(), assess_quality(), review_quality_ctrl(), semantic_enrichment()

Examples

data(example_data)

# median (numeric) / mode (categorical) imputation of all variables:
res <- impute_missing_values(example_data)

# impute only specific variables:
res <- impute_missing_values(example_data, tumoursize, diabetes,
                             method = "median")

# k-nearest-neighbours imputation (requires the 'VIM' package),
# excluding the identifier from the distance calculation:

if (requireNamespace("VIM", quietly = TRUE)) {
  res <- impute_missing_values(example_data, method = "knn",
                               ignore = patient_id)
}


eHDPrep documentation built on Oct. 21, 2026, 5:10 p.m.