# [R] Problem about trim the space and add space to force the same nchar

Henrik Bengtsson hb at maths.lth.se
Wed May 8 08:31:30 CEST 2002

```For
a <- "abc     "
or
a <- "a  b  c       "

# Replace all multiple spaces with a single space
a <- gsub("[ ]+", " ", a)
# Remove all trailing spaces
a <- gsub("[ ]+\$", "", a)
a <- gsub("^[ ]+", "", a)

Maybe there a more clever ways which I don't about, but the above gives you
the idea. I am not sure what your second question is about but if it is
about formatting output take a look at format(), formatC() and the new
sprintf() in [R] v1.5.0. The latter I haven't tried myself yet, but it
should be the most powerful and easy to use.

Best

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/bioinformatics/

> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of ken_lee
> Sent: Wednesday, May 08, 2002 4:33 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Problem about trim the space and add space to force the
> same nchar
>
>
> Dear all,
>      First, Can I trim the space like a<-"abc     "   ==>  a<-
> "abc"    or a<-"a  b  c       "   ==>  a<-"a  b  c"
>      Second, Can I  generate a vector has the same length (nchar)
> like a<-c("mean  =10   ",
>
>
>   "std       =1.23",
>
>                                                                     "...
>
>
> Best regards
>
> Ken
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-
> 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

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

```