Wolfram Fischer
2005-Apr-11 10:22 UTC
[R] How to change letters after space into capital letters
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
Renaud Lancelot
2005-Apr-11 11:08 UTC
[R] How to change letters after space into capital letters
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)
Barry Rowlingson
2005-Apr-11 11:13 UTC
[R] How to change letters after space into capital letters
Wolfram Fischer wrote:> What is the easiest way to change within vector of strings > each letter after a space into a capital letter?This perl code seems to work: #!/usr/bin/perl $s1="this is a lower-cased string"; $s1=~s/ ([a-z])/ \u\1/g; print $s1."\n"; producing: this Is A Lower-cased String but sticking it in a gsub in R doesn't work, even with perl=TRUE: > s1 [1] "this is a lowercased string" > gsub(" ([a-z])"," \\u\\1",s1,perl=TRUE) [1] "this uis ua ulowercased ustrin" But ?gsub does warn you that perl REs may vary depending on the phase of the moon and the PCRE lib on your system. I've just noticed the missing 'g' on the end. Anyone know where that went? Its looking like a bug in the regex processing: > gsub(" ([a-z])"," \\1",s1,perl=TRUE) [1] "this is a lowercased strin" > gsub(" ([a-z])"," \\1",s1,perl=TRUE) [1] "this is a lowercased st" Anyway, back on topic:> 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.)I think this is what's known as 'Camel Case', because the pattern of upper and lower case looks like humps: http://en.wikipedia.org/wiki/Camel_case Baz
Gabor Grothendieck
2005-Apr-11 11:31 UTC
[R] How to change letters after space into capital letters
On Apr 11, 2005 6:22 AM, Wolfram Fischer <wolfram at fischer-zim.ch> wrote:> 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.) >Look for the thread titled String manipulation---mixed case in the r-help archives.
Martin Maechler
2005-Apr-11 15:28 UTC
[R] How to change letters after space into capital letters
>>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at gmail.com> >>>>> on Mon, 11 Apr 2005 07:31:00 -0400 writes:Gabor> On Apr 11, 2005 6:22 AM, Wolfram Fischer <wolfram at fischer-zim.ch> wrote: >> 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.) >> Gabor> Look for the thread titled Gabor> String manipulation---mixed case Gabor> in the r-help archives. Indeed! Thank you, Gabor. If you use R 2.1.0 beta (which you should consider seriously as a good netizen ;-), this is as simple as > RSiteSearch("String manipulation---mixed case") A search query has been submitted to http://search.r-project.org The results page should open in your browser shortly Martin.
Gabor Grothendieck
2005-Apr-11 15:58 UTC
[R] How to change letters after space into capital letters
On Apr 11, 2005 11:28 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote:> > If you use R 2.1.0 beta (which you should consider seriously as > a good netizen ;-), this is as simple as > > > RSiteSearch("String manipulation---mixed case") > A search query has been submitted to http://search.r-project.org > The results page should open in your browser shortlyIt would be nice if RSiteSearch were an entry in the Help menu in the Windows GUI.
McGehee, Robert
2005-Apr-12 17:30 UTC
[R] How to change letters after space into capital letters
This short function capitalizes the first letter of each word in a character string, which is what I think you want. capitalize <- function(x) { x <- strsplit(x, " ") for (i in seq(along = x)) { substr(x[[i]], 1, 1) <- toupper(substr(x[[i]], 1, 1)) } sapply(x, function(z) paste(z, collapse = " ")) } Robert -----Original Message----- From: Wolfram Fischer [mailto:wolfram at fischer-zim.ch] Sent: Monday, April 11, 2005 6:22 AM To: r-help at stat.math.ethz.ch Subject: [R] How to change letters after space into capital letters 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