[R] Applying "toupper" to only portions of text strings

Gabor Grothendieck ggrothendieck at gmail.com
Fri May 27 01:30:13 CEST 2011


On Thu, May 26, 2011 at 6:05 PM, Dennis Fisher <fisher at plessthan.com> wrote:
> Colleagues
>
> Assume that I have a vector containing some text strings, some of which contain a particular character.  I could like to apply "toupper" to the text before the character.  For example (in this case, "|" is the particular character):
>
> ORIGINAL:
>        TEXT    <- c("aaaa", "bbb|cc", "|ddd")
>
> AFTER APPLICATION OF toupper:
>        TEXT    <- c("AAAA", "BBB|cc", "|dddd")
>
> I could loop through each element, strsplit at the character, apply toupper to the first component, then paste each element together.  But, I hope that there is a simpler means to accomplish this.
>

Try this:

> library(gsubfn)
> gsubfn("^[^|]+", toupper, TEXT)
[1] "AAAA"   "BBB|cc" "|dddd"

The regular expression starts from the beginning of the string (i.e.  ^ ) and
includes any immediately following characters that are not | (i.e.  [^|]+ ).
-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list