removing trailing spaces with gsub in R [duplicate]

可紊 提交于 2019-12-03 14:08:03

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::
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!