[R] String manipulation---mixed case

Martin Maechler maechler at stat.math.ethz.ch
Mon Dec 6 15:23:19 CET 2004


>>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at myway.com>
>>>>>     on Mon, 6 Dec 2004 12:10:42 +0000 (UTC) writes:

    Gabor> Gabor Grothendieck <ggrothendieck <at> myway.com> writes:
    Gabor> : 
    Gabor> : Martin Maechler <maechler <at> stat.math.ethz.ch> writes:

           ............

    Gabor> : : Nice.  Since this has been asked before,
    Gabor> : : and it is something common enoguh that Emacs even has this on a
    Gabor> : : key (M-c), I think it's worth making a small example on the help
    Gabor> : : page for toupper/tolower:
    Gabor> : : 
    Gabor> : : ## "Mixed Case" Capitalizing Function :
    Gabor> : : capitalize <- function(x) {
    Gabor> : :   ## toupper( every first letter of a word ) :
    Gabor> : :   s <- strsplit(x, " ")[[1]]
    Gabor> : :   paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ")
    Gabor> : : }
    Gabor> : : capitalize("the quick red fox jumps over the lazy brown dog")
    Gabor> : : ## ->  [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog"
    Gabor> : 
    Gabor> : or the following variation which is about the same length but
    Gabor> : also (1) handles vectors of strings and (2) does not depend
    Gabor> : on the input being lower case:

(1) is desirable, but makes the example harder to understand.
    Many people have been lamenting about too complicated
    examples on help pages.  So, I wouldn't be sure to want the
    improvement as a help example.

(2) is not really desirable; at least not always:

   > ss <- c(first= "ABC and XYZ, now I know ..", second="AIC or BIC or AICc ?")
   > capwords(ss)
                          first                       second 
   "Abc And Xyz, Now I Know .."       "Aic Or Bic Or Aicc ?" 


    Gabor> : capwords <- function(s) {
    Gabor> [...]

    Gabor> Correction.  Should be:

    Gabor> capwords <- function(s) {
    Gabor>   cap <- function(s) paste( toupper(substring(s,1,1)), 
    Gabor>                             tolower(substring(s,2)), sep = "", collapse = " " )
    Gabor>   sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
    Gabor> }

Really nice, but something that most readers of help(toupper)
won't be able to grasp..

[ and when you are really striving for a ``most-short'' one,
  you may want to know that in this case, substr(.) can be used
  instead of substring()  ;-)
]
Martin




More information about the R-help mailing list