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.
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
.
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.
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)
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::
来源:https://stackoverflow.com/questions/10502787/removing-trailing-spaces-with-gsub-in-r