haotu : an open lab notebook

2011/12/02

Trim Remove trailing and leading spaces from a character String in R

Filed under: R — S @ 13:34


# 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)

http://stackoverflow.com/questions/2261079/whitespace-in-r

Blog at WordPress.com.