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.5999890 0.8903502 0.93529980 0.1422943        NA
#> CpG2 0.3328235 0.9144382 0.30122890 0.5492847        NA
#> CpG3 0.4886130        NA 0.06072057 0.9540912 0.3077200
#> CpG4 0.9544738 0.4106898 0.94772694 0.5854834 0.2197676
#> CpG5        NA 0.1470947         NA 0.4045103 0.3694889

# Impute missing values with row medians
impute(X)
#>        Sample1   Sample2    Sample3   Sample4   Sample5
#> CpG1 0.5999890 0.8903502 0.93529980 0.1422943 0.7451696
#> CpG2 0.3328235 0.9144382 0.30122890 0.5492847 0.4410541
#> CpG3 0.4886130 0.3981665 0.06072057 0.9540912 0.3077200
#> CpG4 0.9544738 0.4106898 0.94772694 0.5854834 0.2197676
#> CpG5 0.3694889 0.1470947 0.36948887 0.4045103 0.3694889

# Impute missing values with arbitrary function
impute(X, fun = function(x) { -1} )
#>         Sample1    Sample2     Sample3   Sample4    Sample5
#> CpG1  0.5999890  0.8903502  0.93529980 0.1422943 -1.0000000
#> CpG2  0.3328235  0.9144382  0.30122890 0.5492847 -1.0000000
#> CpG3  0.4886130 -1.0000000  0.06072057 0.9540912  0.3077200
#> CpG4  0.9544738  0.4106898  0.94772694 0.5854834  0.2197676
#> CpG5 -1.0000000  0.1470947 -1.00000000 0.4045103  0.3694889