How can I trim leading and trailing white space?

后端 未结 13 1710
北海茫月
北海茫月 2020-11-22 03:53

I am having some troubles with leading and trailing white space in a data.frame.

For example, I like to take a look at a specific row in a data.fra

13条回答
  •  滥情空心
    2020-11-22 04:22

    Use grep or grepl to find observations with white spaces and sub to get rid of them.

    names<-c("Ganga Din\t", "Shyam Lal", "Bulbul ")
    grep("[[:space:]]+$", names)
    [1] 1 3
    grepl("[[:space:]]+$", names)
    [1]  TRUE FALSE  TRUE
    sub("[[:space:]]+$", "", names)
    [1] "Ganga Din" "Shyam Lal" "Bulbul"
    

提交回复
热议问题