Skip to contents

Replaces empty strings.

Usage

replace_empty(x, ...)

# S4 method for matrix
replace_empty(x, value)

# S4 method for 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,] "O"  "D"  "D"  "Z"  "Q" 
#> [2,] ""   "G"  "L"  "G"  ""  
#> [3,] "C"  "J"  ""   "U"  "D" 
#> [4,] "I"  "S"  "Z"  "T"  "A" 
#> [5,] "N"  "B"  "B"  "A"  "Q" 

## Remove rows with empty strings
remove_empty(X, margin = 1)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,] "O"  "D"  "D"  "Z"  "Q" 
#> [2,] "I"  "S"  "Z"  "T"  "A" 
#> [3,] "N"  "B"  "B"  "A"  "Q" 

## Replace empty strings
replace_empty(X, value = "XXX")
#>      [,1]  [,2] [,3]  [,4] [,5] 
#> [1,] "O"   "D"  "D"   "Z"  "Q"  
#> [2,] "XXX" "G"  "L"   "G"  "XXX"
#> [3,] "C"   "J"  "XXX" "U"  "D"  
#> [4,] "I"   "S"  "Z"   "T"  "A"  
#> [5,] "N"   "B"  "B"   "A"  "Q"