Re: [R] removing blanks from a string

From: john seers (IFR) <>
Date: Fri, 27 Jun 2008 11:56:01 +0100



Yes, I am sure you are right. I guessed the intended question was not so specific.

I have always thought of spaces and blanks as the same thing. And use "white space" for a more general catch all description for tabs etc.

Is "white space" and "spaces" the same thing?



-----Original Message-----
From: Prof Brian Ripley [] 
Sent: 27 June 2008 11:10
To: john seers (IFR)
Cc: jim holtman; juli pausas; R-help
Subject: Re: [R] removing blanks from a string

There are well-informed answers given as examples on the sub() help

Hint 1: there is no need to globally substitute patterns anchored at the
end: they can only match in one place.  There is also no need to
substitute "" for "".

Hint 2: 'blank characters' and 'spaces' are not the same thing.
[:blank:] seems the relevant character class, so

sub("[[:blank:]]+$", "", x)

seems an accurate answer to the question asked (which might not be the
question intended).

On Fri, 27 Jun 2008, john seers (IFR) wrote:

> There is also the "trim" command in the gdata package. Removes blanks
> from the front of the string as well which may not be what you want.
But that removes *spaces* and not *blanks*, according to the help page (and the actual code -- in fact it removes only ASCII space characters and not others such as nbspace).
> Regards
> JS

> -----Original Message-----
> From:
> On Behalf Of jim holtman
> Sent: 27 June 2008 10:27
> To: juli pausas
> Cc: R-help
> Subject: Re: [R] removing blanks from a string
> Is this what you want:
>> x <- c("hola ", "Yes ", "hello ")
>> gsub(" *$", "", x)
> [1] "hola" "Yes" "hello"
> On Fri, Jun 27, 2008 at 4:34 AM, juli pausas <> wrote:
>> Hi
>> Is there a way to remove blank characters from the end of strings in
>> vector? Something like the =TRIM functions of the OpenOffice
>> spreadsheet. E.g.,
>> a <- c("hola ", "Yes ", "hello ") # I'd like to get:
>> c("hola", "Yes", "hello")
>> Thanks
>> Juli
-- Brian D. Ripley, Professor of Applied Statistics, University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 ______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
Received on Fri 27 Jun 2008 - 11:00:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 27 Jun 2008 - 11:31:13 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive