[R] Trim function ?

Douglas Bates bates at stat.wisc.edu
Thu May 9 17:59:22 CEST 2002


Ernesto Jardim <ernesto at ipimar.pt> writes:

> Hi
> 
> I've imported some data from a oracle database and the strings have a
> lot of blank space (because of the delimiter). I want to remove these
> blanks space with something similar to a trim function.
> 
> I don't find a trim function or something similar. Can someone give a
> help on this ?

If you want to remove trailing blanks you can use sub with the regular
expression ' +$'. 

> str = 'Now is the time      '
> sub(' +$', '', str, extended = TRUE)
[1] "Now is the time"

I had hoped you could use '\s+$' or '\\s+$' but that doesn't seem to
work.  I guess we would need the perl regular expression library, of
which there has been some discussion, to do that.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list