Skip to contents

Replaces empty strings.

Usage

replace_empty(x, ...)

# S4 method for class 'matrix'
replace_empty(x, value)

# S4 method for class 'data.frame'
replace_empty(x, value)

Arguments

x

An R object (should be a matrix or a data.frame).

...

Currently not used.

value

A possible replacement value.

See also

Author

N. Frerebeau

Examples

## Create a data matrix
X <- matrix(sample(LETTERS, 25, TRUE), nrow = 5, ncol = 5)

## Add empty string
k <- sample(1:25, 3, FALSE)
X[k] <- ""
X
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] "P"  ""   "X"  "F"  "P" 
#> [2,] ""   "K"  "X"  "V"  "J" 
#> [3,] "W"  "M"  "Q"  "C"  "Y" 
#> [4,] "Z"  ""   "C"  "X"  "A" 
#> [5,] "B"  "X"  "T"  "K"  "B" 

## Remove rows with empty strings
remove_empty(X, margin = 1)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] "W"  "M"  "Q"  "C"  "Y" 
#> [2,] "B"  "X"  "T"  "K"  "B" 

## Replace empty strings
replace_empty(X, value = "XXX")
#>      [,1]  [,2]  [,3] [,4] [,5]
#> [1,] "P"   "XXX" "X"  "F"  "P" 
#> [2,] "XXX" "K"   "X"  "V"  "J" 
#> [3,] "W"   "M"   "Q"  "C"  "Y" 
#> [4,] "Z"   "XXX" "C"  "X"  "A" 
#> [5,] "B"   "X"   "T"  "K"  "B"