Re: [R] String manipulation---mixed case

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon 06 Dec 2004 - 22:08:29 EST

>>>>> "Spencer" == Spencer Graves <spencer.graves@pdf.com>
>>>>> on Sun, 05 Dec 2004 13:48:07 -0800 writes:

    Spencer> That's great, Peter. 
    Spencer> For pedestrians like me who are not quite as facile with regular 
    Spencer> expressions, the following seems slightly more readable: 

    Spencer> s <- "the quick red fox jumps over the lazy brown dog"
    Spencer> ss <- strsplit(s, " ")[[1]]
    Spencer> ss1 <- substring(ss, 1,1)

    Spencer> ss2 <- substring(ss, 2)
    Spencer> paste(toupper(ss1), ss2, sep="", collapse=" ")

    Spencer> [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog"

Nice. Since this has been asked before, and it is something common enoguh that Emacs even has this on a key (M-c), I think it's worth making a small example on the help page for toupper/tolower:

## "Mixed Case" Capitalizing Function :
capitalize <- function(x) {
  ## toupper( every first letter of a word ) :
  s <- strsplit(x, " ")[[1]]
  paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ") }
capitalize("the quick red fox jumps over the lazy brown dog") ## -> [1] "The Quick Red Fox Jumps Over The Lazy Brown Dog"



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Dec 06 22:30:04 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 01:22:34 EST