[R] Better way to change the name of a column in a dataframe?

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Thu Dec 14 23:24:08 CET 2006


Hi,

names(frame)[names(frame) == "cmlo3"] <- "col3"

should also work

Cheers

Andrew

On Thu, Dec 14, 2006 at 05:05:20PM -0500, Mike Prager wrote:
> "Ben Fairbank" <BEN at SSANET.COM> wrote:
> 
> > [...] I want to correct or otherwise change the name of one of the
> > columns, I can do so with 
> > 
> > > dimnames(frame)[[2]][which(dimnames(frame)[[2]]=="cmlo3")] <- "col3"
> > 
> > This seems cumbersome and not very intuitive.  How can one accomplish
> > this more simply?
> 
> This is slightly simpler than what you had:
> 
>       names(frame)[which(names(frame) == "cmlo3")] <- "col3"
> 
> There are probably better ways still. 
> 
> -- 
> Mike Prager, NOAA, Beaufort, NC
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-9763
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
http://www.ms.unimelb.edu.au/~andrewpr
http://blogs.mbs.edu/fishing-in-the-bay/



More information about the R-help mailing list