[R] How to change letters after space into capital letters

Renaud Lancelot renaud.lancelot at cirad.fr
Mon Apr 11 13:08:34 CEST 2005


Wolfram Fischer a écrit :

> What is the easiest way to change within vector of strings
> each letter after a space into a capital letter?
> 
> E.g.:
>   c( "this is an element of the vector of strings", "second element" )
> becomes:
>   c( "This Is An Element Of The Vector Of Strings", "Second Element" )
> 
> My reason to try to do this is to get more readable abbreviations.
> (A suggestion would be to add an option to abbreviate() which changes
> letters after space to uppercase letters before executing the abbreviation
> algorithm.)
> 
> Thanks - Wolfram
> 
> ______________________________________________
> R-help at 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
> 

cap.leading <- function (string) {
   fn <- function(x) {
       v <- unlist(strsplit(x, split = " "))
       u <- sapply(v, function(x) {
           x <- tolower(x)
           substring(x, 1, 1) <- toupper(substring(x, 1, 1))
           x
       })
     paste(u, collapse = " ")
     }
   unname(sapply(string, fn))
   }

 > cap.leading(c( "this is an element of the vector of strings", "second 
element" ))
[1] "This Is An Element Of The Vector Of Strings" "Second Element"

Best,

Renaud

-- 
Dr Renaud Lancelot, vétérinaire
C/0 Ambassade de France - SCAC
BP 834 Antananarivo 101 - Madagascar

e-mail: renaud.lancelot at cirad.fr
tel.:   +261 32 40 165 53 (cell)
         +261 20 22 665 36 ext. 225 (work)
         +261 20 22 494 37 (home)




More information about the R-help mailing list