8

Does anyone have a trick to remove trailing spaces on variables with gsub?

Below is a sample of my data. As you can see, I have both trailing spaces and spaces embedded in the variable.

county <- c("mississippi ","mississippi canyon","missoula ",
            "mitchell ","mobile ", "mobile bay")  

I can use the following logic to remove all spaces, but what I really want is to only move the spaces at the end.

county2 <- gsub(" ","",county)

Any assistance would be greatly appreciated.

MikeTP
  • 7,716
  • 16
  • 44
  • 57

4 Answers4

32

Read ?regex to get an idea how regular expressions work.

gsub("[[:space:]]*$","",county)

[:space:] is a pre-defined character class that matches space characters in your locale. * says to repeat the match zero or more times and $ says to match the end of the string.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • 3
    @MikeTP Hadly nicely summarized a bunch of common string functions in his package `stringr`. The one that does this is `str_trim()`. – Justin May 08 '12 at 20:58
13

You could use an regular expression:

 county <- c("mississippi ","mississippi canyon","missoula ",
        "mitchell ","mobile ", "mobile bay")  
 county2 <- gsub(" $","", county, perl=T)

$ stand for the end of your text sequence, therefore only trailing spaces are matched. perl=T enables regular expressions for the match pattern. For more on regular expression see ?regex.

smu
  • 8,757
  • 2
  • 19
  • 14
  • 5
    @MikeTP: note that this will only work if there is only a single trailing space. If your `county` vector has an element with multiple trailing spaces, only the last will be removed. – Joshua Ulrich May 08 '12 at 17:50
8

If you don't need to use the gsub command - the str_trim function is useful for this.

    library(stringr)
    county <- c("mississippi ","mississippi canyon","missoula ",
        "mitchell ","mobile ", "mobile bay")
    str_trim(county)
MatthewR
  • 2,660
  • 5
  • 26
  • 37
0
Above solution can not be generalized. Here is an example:


    a<-" keep business moving"
    str_trim(a) #Does remove trailing space in a single line string

However str_trim() from 'stringr' package works only for a vector of words   and a single line but does not work for multiple lines based on my testing as consistent with source code reference. 

    gsub("[[:space:]]*$","",a) #Does not remove trailing space in my example
    gsub(" $","", a, perl=T) #Does not remove trailing space in my example

Below code works for both term vectors and or multi-line character vectors   which was provided by the reference[1] below. 

    gsub("^ *|(?<= ) | *$", "", a, perl=T)


#Reference::
  • I'm trying to understand this, but for me there is only a leading space, not a trailing space? – TobiO Jul 21 '17 at 07:29