[R] How to rename columns that start with numbers?

John Kane jrkrideau at yahoo.ca
Fri Aug 14 00:36:00 CEST 2009


Something like this. Create empty vector of correct length, paste values into it and then use names(mydata) <- myvector.

x  <- data.frame(aa=1:10, bb=letters[1:10])

myvector <- c(rep(NA,length(x)))
for (i in 1:length(names(x))) {
   myvector[i] <- paste("SURV_",i, sep="")
   }


--- On Thu, 8/13/09, Mark Na <mtb954 at gmail.com> wrote:

> From: Mark Na <mtb954 at gmail.com>
> Subject: [R] How to rename columns that start with numbers?
> To: r-help at r-project.org
> Received: Thursday, August 13, 2009, 6:24 PM
> Hello,
> 
> My dataframe has new columns that start with the number 1
> or 2 (resulting
> from a reshape cast command).
> 
> Instead of having these columns automatically renamed by R
> so start with the
> letter X, I would like to rename these columns to start
> with the characters
> "SURV_" (e.g., SURV_1, SURV_2).
> 
> I can't seen to use grep() to identify and rename the
> columns starting with
> either 1 or 2.
> 
> Any help would be much appreciated, thanks!
> 
> (I know I could rename these manually, but the above is a
> simpler statement
> of the actual problem, which involves several dozen
> columns, so that's why
> I'd prefer not to so it manually)
> 
> Mark Na
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 


      __________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now
http://ca.toolbar.yahoo.com.




More information about the R-help mailing list