Skip to contents

Replaces missing or infinite values or zeros.

Usage

replace_NA(x, ...)

replace_Inf(x, ...)

replace_zero(x, ...)

# S4 method for matrix
replace_NA(x, value = 0)

# S4 method for matrix
replace_Inf(x, value = 0)

# S4 method for matrix
replace_zero(x, value)

Arguments

x

A matrix, a data.frame or a *Matrix object.

...

Currently not used.

value

A possible value to replace missing or infinite values of x.

See also

Other data cleaning tools: count(), detect(), remove()

Author

N. Frerebeau

Examples

## Create a count data matrix
X <- matrix(sample(1:10, 25, TRUE), nrow = 5, ncol = 5)

## Add NA
k <- sample(1:25, 3, FALSE)
X[k] <- NA
X
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    3    6    4    9
#> [2,]    9   NA    5    9    9
#> [3,]    7    5    3    3   NA
#> [4,]    1    9   10    8    8
#> [5,]    8    3    1   10   NA

## Count missing values in rows
count(X, f = is.na, margin = 1)
#> [1] 0 1 1 0 1
## Count non-missing values in columns
count(X, f = is.na, margin = 2, negate = TRUE)
#> [1] 5 4 5 5 3

## Find row with NA
detect(X, f = is.na, margin = 1)
#> [1] FALSE  TRUE  TRUE FALSE  TRUE
## Find column without any NA
detect(X, f = is.na, margin = 2, negate = TRUE, all = TRUE)
#> [1]  TRUE FALSE  TRUE  TRUE FALSE

## Remove row with any NA
compact(X, f = is.na, margin = 1, all = FALSE)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    3    6    4    9
#> [2,]    1    9   10    8    8
## Remove column with any NA
compact(X, f = is.na, margin = 2, all = FALSE)
#>      [,1] [,2] [,3]
#> [1,]    2    6    4
#> [2,]    9    5    9
#> [3,]    7    3    3
#> [4,]    1   10    8
#> [5,]    8    1   10

## Replace NA with zeros
replace_NA(X, value = 0)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    2    3    6    4    9
#> [2,]    9    0    5    9    9
#> [3,]    7    5    3    3    0
#> [4,]    1    9   10    8    8
#> [5,]    8    3    1   10    0