[R] Removing trailing spaces

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Dec 22 00:36:47 CET 2004


Patrick Connolly <p.connolly at hortresearch.co.nz> writes:

> Some years ago when I used S-PLUS, I seem to remember, there was a
> discussion about a simple method of removing trailing spaces from
> vector elements.  I'd be fairly sure the same would work in R, but I
> can't find any mention of anything like it in the R archives or with
> help.search().
> 
> There are ways I could do it with substring(), but I seem to remember
> there was something more elegant.  I can find the strip function in
> the clim.pact package, but that trims everything after the first space
> which is not useful in this case.
> 
> Someone please refresh my memory.

Like this?

> x <- c("a b ", "dfhskf   ", "FGF", "IUOI UOUO UOUO  ")
> sub(" *$", "", x)
[1] "a b"            "dfhskf"         "FGF"            "IUOI UOUO UOUO"


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list