Friday, April 22, 2016

How to trim leading and trailing whitespace in R?

Probably the best way is to handle the trailing whitespaces when you read your data file. If you use read.csv or read.table you can set the parameterstrip.white=TRUE.

If you want to clean strings afterwards you could use one of these functions:

# 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)
To use one of these functions on myDummy$country:

 myDummy$country <- trim(myDummy$country)
To 'show' the whitespace you could use:

 paste(myDummy$country)
which will show you the strings surrounded by quotation marks (") making whitespaces easier to spot.


rim {gdata}R Documentation

Remove leading and trailing spaces from character strings

Description

Remove leading and trailing spaces from character strings and other related objects.

Usage

trim(s, recode.factor=TRUE, ...)

bob <- data.frame(lapply(bob, as.character), stringsAsFactors=FALSE)


bob[] <- lapply(bob, as.character)

No comments:

Post a Comment