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

From: Renaud Lancelot <renaud.lancelot_at_cirad.fr>
Date: Mon 11 Apr 2005 - 21:08:34 EST

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@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@cirad.fr
tel.:   +261 32 40 165 53 (cell)
         +261 20 22 665 36 ext. 225 (work)
         +261 20 22 494 37 (home)

______________________________________________
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 Apr 11 21:23:18 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:06 EST