[R] String manipulation---mixed case
Gabor Grothendieck
ggrothendieck at myway.com
Sun Dec 5 16:46:57 CET 2004
Damian Betebenner <damian.betebenner <at> bc.edu> writes:
:
: Hello,
:
: Does anyone know of a "slick" way to get R to convert a string which is all
upper case to a string where the first
: letter in each word is upper case and all others are lower case?
:
: I suspect the solution is to begin by parsing the string, convert the
appropriate letters to upper and lower
: case using "toupper" and "tolower", and then to paste the pieces back
together. Moreover, given the
: elegance and power of R, I'll bet this can be coded very tersely. Before I
embarked on trying to code this, I
: thought I would tap the R braintrust to see if this has already been done.
:
Assuming x contains your string (or a vector of such strings), try this.
The first iteration replaces each A not following a word boundary
with a. The second does the analogous operation with B, etc.
for(L in LETTERS) x <- gsub(paste("\\B", L, sep = ""), tolower(L), x)
More information about the R-help
mailing list