removing trailing spaces with gsub in R [duplicate]

强颜欢笑 提交于 2019-12-04 21:32:56

问题


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.


回答1:


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.




回答2:


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.




回答3:


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)



回答4:


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

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