[R] removing blanks from a string
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jun 27 13:07:26 CEST 2008
On Fri, 27 Jun 2008, john seers (IFR) wrote:
> Yes, I am sure you are right. I guessed the intended question was not so
> 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?
No. There are
spaces [e.g. ASCII space]
blank characters [e.g. ASCII space and tab]
whitespace [e.g. ASCII space, tab and newline]
that are usually increasingly more general. But CR, FF and VT are
normally whitespace, and internationalized programs will generally allow
other space characters, including nbspace (and Unicode category Zs).
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> 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).
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org]
>> 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)
>>  "hola" "Yes" "hello"
>> On Fri, Jun 27, 2008 at 4:34 AM, juli pausas <pausas at gmail.com> wrote:
>>> 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")
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help