Learn R Programming

jwutil (version 1.2.3)

strip: strip all whitespace

Description

could do this with regular expression, but slow, and this function is called frequently. My only use case works with removal of all space character whitespace, and I don't expect <TAB>. This uses non-unicode aware matching for speed. This can be changed by setting useBytes to FALSE.

Usage

strip(x, pattern = " ", useBytes = TRUE)

Arguments

x

is a character vector to strip

pattern

is the non-regex of the character to strip, default " "

useBytes

logical scalar. Unlike gsub, this will default to TRUE here, therefore breaking unicode.

Value

character vector

Details

gsub is probably quicker than stringr/stringi. For comorbidity processing, this package prefers the faster base functions, whereas stringr is used for tasks which are not time critical, e.g. parsing source data to be included in the distributed icd package.

Examples

Run this code
# NOT RUN {
requireNamespace("microbenchmark")
requireNamespace("stringr")
x <- random_string(25000)
microbenchmark::microbenchmark(
  gsub(x = x, pattern = "A", replacement = "", fixed = TRUE, useBytes = TRUE),
  gsub(x = x, pattern = "A", replacement = "", fixed = TRUE, useBytes = TRUE, perl = TRUE),
  gsub(x = x, pattern = "A", replacement = ""),
  stringr::str_replace_all(x, "A", "")
)
# }

Run the code above in your browser using DataLab