Remove Leading and Trailing Whitespace in R

The trimws() function can be used to remove leading and trailing whitespace from a character string in R:

trimws(my_string)

Example of Removing Leading and Trailing Whitespace in R

Here is an example of removing leading and trailing whitespace from a character string in R:

# Example of a string that contains leading and trailing whitespace
my_string <- "  This is a string that contains leading and trailing whitespace.  "

# Remove leading and trailing whitespace
trimmed_string <- trimws(my_string)

# Print the result
print(trimmed_string)

The result after the removal of the leading and trailing whitespace:

"This is a string that contains leading and trailing whitespace."

Leave a Comment