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

From: Eik Vettorazzi <E.Vettorazzi_at_uke.uni-hamburg.de>
Date: Fri, 27 May 2011 16:02:39 +0200

or even shorter

 TEXT <- c("aaaa", "bbb|cc", "|ddd")
 gsub("(^.*[|])","\\U\\1",TEXT,perl=T)

Am 27.05.2011 00:24, schrieb Phil Spector:

> Dennis -
>     Here's one way, using a somewhat obscure feature of perl
> regular expressions, i.e. the \U and \L escape characters which
> modify the case of the strings they appear in:
> 

>> TEXT <- c("aaaa", "bbb|cc", "|ddd")
>> sub('([a-z]*)(\\|?)([a-z]*)','\\U\\1\\2\\L\\3',TEXT,perl=TRUE)
> [1] "AAAA"   "BBB|cc" "|ddd"
> 
>                     - Phil Spector
>                      Statistical Computing Facility
>                      Department of Statistics
>                      UC Berkeley
>                      spector_at_stat.berkeley.edu
> 
> 
> 
> On Thu, 26 May 2011, Dennis Fisher 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.
>>
>> Thanks in advance.
>>
>> Dennis
>>
>>
>> Dennis Fisher MD
>> P < (The "P Less Than" Company)
>> Phone: 1-866-PLessThan (1-866-753-7784)
>> Fax: 1-866-PLessThan (1-866-753-7784)
>> www.PLessThan.com
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 27 May 2011 - 14:05:23 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 27 May 2011 - 15:00:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive