[R] How to change ONLY the first character of each variable name

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Jun 19 12:51:31 CEST 2009


Henrique Dallazuanna wrote:
> Try this:
>
> gsub("^M{1}", "MOLE", names(data))
>   

{1} is inessential here.

vQ

> On Thu, Jun 18, 2009 at 8:24 PM, Mark Na <mtb954 at gmail.com> wrote:
>
>   
>> Dear R-helpers,
>>
>> I would like to adapt the following code
>>
>>     
>>> names(data)<-sub("M","MOLE",names(data))
>>>       
>> which changes any occurrence of "M" (in my variable names) to "MOLE"
>>
>> such that it ONLY operates on the first character of each variable
>> name, i.e. M will only be changed to MOLE if it's the first character
>> of a variable.
>>
>> I would appreciate any help you might provide. Thanks!
>>
>> Mark Na
>>
>> ______________________________________________
>> 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.
>




More information about the R-help mailing list