[R] removing blanks from a string

john seers (IFR) john.seers at bbsrc.ac.uk
Fri Jun 27 12:56:01 CEST 2008


 

Hello 

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? 


Regards

JS





 
---
-----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
page.

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: 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)
> [1] "hola"  "Yes"   "hello"
>>
>>
>
>
> On Fri, Jun 27, 2008 at 4:34 AM, juli pausas <pausas at gmail.com> wrote:
>> Hi
>> Is there a way to remove blank characters from the end of strings in
a
>
>> 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,                  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 mailing list