Skip to contents

Remove Leading/Trailing Whitespace

Usage

clean_whitespace(x, ...)

# S4 method for data.frame
clean_whitespace(x, which = c("both", "left", "right"), squish = TRUE)

# S4 method for matrix
clean_whitespace(x, which = c("both", "left", "right"), squish = TRUE)

Arguments

x

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

...

Currently not used.

which

A character string specifying whether to remove both leading and trailing whitespace (default), or only leading ("left") or trailing ("right").

squish

A logical scalar: should all internal whitespace be replaced with a single space?

Author

N. Frerebeau

Examples

x <- data.frame(
  A = c("  Both ", "  Left", "Right  "),
  B = 1:3
)

clean_whitespace(x, which = "both")
#>       A B
#> 1  Both 1
#> 2  Left 2
#> 3 Right 3
clean_whitespace(x, which = "left")
#>        A B
#> 1  Both  1
#> 2   Left 2
#> 3 Right  3
clean_whitespace(x, which = "right")
#>       A B
#> 1  Both 1
#> 2  Left 2
#> 3 Right 3