How can we trim leading and trailing whitespaces in R

0 votes
Apr 18, 2018 in Data Analytics by shams
• 3,670 points
1,988 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,790 points

Related Questions In Data Analytics

+1 vote
2 answers

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
80,546 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,670 points
5,931 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 27, 2018 in Data Analytics by shams
• 3,670 points
6,137 views
0 votes
1 answer

How can I remove duplicated rows in R ?

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

answered Apr 27, 2018 in Data Analytics by shams
• 3,670 points
2,912 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 Sahiti
• 6,370 points
24,566 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,670 points
1,233 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,790 points
1,833 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,790 points
1,039 views
+1 vote
2 answers

How can we count TRUE values in a logical vector?

Hi, You can get a count of all ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
10,008 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,790 points
945 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP