[R] Function for trim blanks from a string(s)?
marc_schwartz at comcast.net
Mon Aug 6 21:40:17 CEST 2007
On Mon, 2007-08-06 at 12:15 -0700, adiamond wrote:
> I feel like an idiot posting this because every language I've ever seen has a
> string function that trims blanks off strings (off the front or back or
> both). Ideally, it would process whole data frames/matrices etc but I don't
> even see one that processes a single string. But I've searched and I don't
> even see that. There's a strtrim function but it does something completely
If you want to do this while initially importing the data into R using
one of the read.table() family of functions, see the 'strip.white'
argument in ?read.table, which would do an entire data frame in one
Otherwise, the easiest way to do it would be to use sub() or gsub()
along the lines of the following:
# Strip leading space
sub("^ +", "", YourTextVector)
# Strip trailing space
sub(" +$", "", YourTextVector)
# Strip both
gsub("(^ +)|( +$)", "", YourTextVector)
Examples of use:
> sub("^ +", "", " Leading Space")
 "Leading Space"
> sub(" +$", "", "Trailing Space ")
 "Trailing Space"
> gsub("(^ +)|( +$)", "", " Leading and Trailing Space ")
 "Leading and Trailing Space"
See ?sub which also has ?gsub
Note that the above will only strip spaces, not all white space.
You can then use the appropriate call in one of the *apply() family of
functions to loop over columns/rows as may be appropriate.
More information about the R-help