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

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Mon Apr 11 13:13:41 CEST 2005


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




More information about the R-help mailing list