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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
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 <[EMAIL PROTECTED]> 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,                  [EMAIL PROTECTED]
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@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.

Reply via email to