Replaces NA values in a numeric matrix with the column median.

impute_na_median(mat)

Arguments

mat

Numeric matrix that may contain NA values.

Value

Numeric matrix with NA values replaced by column medians.

Details

For each column, NA values are replaced with the median of non-NA values. If a column is entirely NA, it remains NA (a warning should be issued by the caller).

Examples

mat <- matrix(c(1, 2, NA, 4, NA, 6), ncol = 2)
impute_na_median(mat)
#>      [,1] [,2]
#> [1,]  1.0    4
#> [2,]  2.0    5
#> [3,]  1.5    6