trimws {base} //Remove Leading/Trailing Whitespace
Removes leading and/or trailing whitespace from character strings.
Usage
trimws(x, which = c("both", "left", "right"))
Arguments
x
a character vector
which
a character string specifying whether to remove both leading and trailing whitespace (default), or only leading ("left") or trailing ("right"). Can be abbreviated.
Details
For portability, ‘whitespace’ is taken as the character class [ \t\r\n] (space, horizontal tab, line feed, carriage return).
Examples
x <- " Some text. "
x
trimws(x)
trimws(x, "l")
trimws(x, "r")
The stringr package has a handy function str_trim (edited) that comes to the rescue and is straightforward to use.
First make sure that the package is available in the R session