How can we trim leading and trailing whitespaces in R?

0 votes
Apr 18, 2018 in Data Analytics by shams
• 3,580 points
358 views

1 answer to this question.

0 votes
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
answered Apr 18, 2018 by zombie
• 3,690 points

Related Questions In Data Analytics

0 votes
1 answer

Which function can I use to clear the console in R and RStudio ?

Description                   Windows & Linux           Mac Clear console                      Ctrl+L ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
918 views
0 votes
1 answer

How can I define Global Variables in R?

The variables declared inside a function are ...READ MORE

answered Apr 25, 2018 in Data Analytics by shams
• 3,580 points
958 views
0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 26, 2018 in Data Analytics by shams
• 3,580 points
71 views
0 votes
1 answer

How can I remove duplicated rows in R ?

The function distinct() in the dplyr package ...READ MORE

answered Apr 26, 2018 in Data Analytics by shams
• 3,580 points
478 views
0 votes
1 answer

How to add leading zeros in R?

There are several solutions to this. One of ...READ MORE

answered Apr 30, 2018 in Data Analytics by darklord
• 6,140 points
2,623 views
0 votes
1 answer

How can I control the size of points in an R scatterplot?

plot(variable, type='o' , pch=5, cex=.3) The pch argument ...READ MORE

answered May 3, 2018 in Data Analytics by shams
• 3,580 points
24 views
0 votes
1 answer

How can I print string and variable contents on the same line using R?

There are two options for doing so.  You ...READ MORE

answered May 9, 2018 in Data Analytics by zombie
• 3,690 points
23 views
0 votes
1 answer

How can I add x and y axis labels in ggplot2 ?

# Load a dataset(to work with) # We’ll ...READ MORE

answered May 16, 2018 in Data Analytics by zombie
• 3,690 points
87 views
0 votes
1 answer

How can we count TRUE values in a logical vector?

In the cases of problems where logical ...READ MORE

answered Apr 19, 2018 in Data Analytics by zombie
• 3,690 points
801 views
0 votes
1 answer

How can I select a CRAN mirror in R ?

There are many ways of doing so ...READ MORE

answered May 9, 2018 in Data Analytics by zombie
• 3,690 points
28 views