Skip to contents

Impute NA values for each row of a matrix.

Usage

impute(x, fun = median)

Arguments

x

numeric matrix or data.frame that can be converted to a numeric matrix

fun

Imputation function to apply to rows of the matrix. Default median

Value

Matrix with imputed values

Examples

# Create a matrix of values with NAs
X <- matrix(runif(25), 5, dimnames = list(paste0("CpG", 1:5), paste0("Sample", 1:5)))
X[sample.int(25, 5)] <- NA
X
#>         Sample1   Sample2   Sample3    Sample4     Sample5
#> CpG1 0.09897127 0.7845135 0.4774079         NA 0.272025531
#> CpG2 0.06796681 0.2149673        NA 0.26908254 0.990651553
#> CpG3 0.83409118 0.6713936 0.7094873 0.92344269 0.598061029
#> CpG4         NA        NA 0.7027670 0.05848265 0.025522872
#> CpG5         NA 0.4777758 0.1869913 0.59233091 0.004016516

# Impute missing values with row medians
impute(X)
#>         Sample1    Sample2   Sample3    Sample4     Sample5
#> CpG1 0.09897127 0.78451347 0.4774079 0.37471670 0.272025531
#> CpG2 0.06796681 0.21496728 0.2420249 0.26908254 0.990651553
#> CpG3 0.83409118 0.67139356 0.7094873 0.92344269 0.598061029
#> CpG4 0.05848265 0.05848265 0.7027670 0.05848265 0.025522872
#> CpG5 0.33238352 0.47777578 0.1869913 0.59233091 0.004016516

# Impute missing values with row mins
impute(X, min)
#>          Sample1    Sample2    Sample3    Sample4     Sample5
#> CpG1 0.098971266 0.78451347 0.47740786 0.09897127 0.272025531
#> CpG2 0.067966805 0.21496728 0.06796681 0.26908254 0.990651553
#> CpG3 0.834091182 0.67139356 0.70948735 0.92344269 0.598061029
#> CpG4 0.025522872 0.02552287 0.70276704 0.05848265 0.025522872
#> CpG5 0.004016516 0.47777578 0.18699126 0.59233091 0.004016516