azadag
10/21/2016 - 10:22 PM

Trim Whitespace Functions From http://stackoverflow.com/questions/2261079/how-to-trim-leading-and-trailing-whitespace-in-r

# returns string w/o leading whitespace
trim.leading <- function (x)  sub("^\\s+", "", x)

# returns string w/o trailing whitespace
trim.trailing <- function (x) sub("\\s+$", "", x)

# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)

# also in Base R

trimws()

# or stringr
install.packages("stringr", dependencies=TRUE)
require(stringr)
example(str_trim)
d4$clean2<-str_trim(d4$V2)