Re: [R] removing blanks from a string

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri, 27 Jun 2008 11:10:12 +0100 (BST)

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 27 Jun 2008 - 10:35:37 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive