Mark Na
2009-Jun-18 23:24 UTC
[R] How to change ONLY the first character of each variable name
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
(Ted Harding)
2009-Jun-18 23:46 UTC
[R] How to change ONLY the first character of each variable
On 18-Jun-09 23:24:39, Mark Na 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 NaM <- "MATMAN" sub("^M","MOLE",M) # [1] "MOLEATMAN" AM <- "AMATMAN" sub("^M","MOLE",AM) # [1] "AMATMAN" Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 19-Jun-09 Time: 00:46:40 ------------------------------ XFMail ------------------------------
Henrique Dallazuanna
2009-Jun-19 00:03 UTC
[R] How to change ONLY the first character of each variable name
Try this: gsub("^M{1}", "MOLE", names(data)) On Thu, Jun 18, 2009 at 8:24 PM, Mark Na <mtb954@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]