[R] How to change letters after space into capital letters
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Apr 11 14:16:27 CEST 2005
On Mon, 11 Apr 2005, Barry Rowlingson wrote:
> Wolfram Fischer wrote:
>> What is the easiest way to change within vector of strings
>> each letter after a space into a capital letter?
[Example omitted, that did somthing different!
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" )
]
Similarly, Gabor G is referring to a thread which answers a _different_
question.
> 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 that is not what his example does, which capitalized the first word
too. I think he intended s/\b([a-z])/\u\1/g;
gannet% echo "this is a lower-cased string" | perl -p -e 's/\b([a-z])/\u\1/g'
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.
This is not actually to do with REs, but substitutions. \u is not
interpreted in substitutions (and only \1 to \9 are, as documented).
> I've just noticed the missing 'g' on the end. Anyone know where that went?
Yes. Use R-2.1.0 beta which has many bugs in gsub fixed.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list