trimws
Remove leading or trailing white space

Description

Removes from the beginning or end of a character string any spaces, tabs, or other characters that produce white space.

Usage

trimws(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]")

Arguments

x character data. If it is not already character data, it is converted with as.character.
which one of the following words, possibly truncated, specifying where to remove white space.
"both" remove white space from the beginning and the end of x. The default.
"left" remove white space from the beginning of x.
"right" remove white space from the end of x.
whitespace a regular expression specifying what is to be considered whitespace. The default value specifies ASCII space, tab, newline, and linefeed characters.

Details

White space characters include space, tab, newline, return, and a few other characters matched by the regular expression "\s".
Value
returns an object like the input x (subjected to as.character() if it is not character data) with the leading or trailing (or both) white space removed.
See Also
gsub.
Examples
trimws(c("  Male", "Female", "Not Known "))
trimws(c("~~Male", "Female", "Not~Known~~"), whitespace="~")
Package base version 6.0.0-69
Package Index