Re: [R] Column renaming

From: hadley wickham <h.wickham_at_gmail.com>
Date: Mon, 05 May 2008 12:09:12 -0500

On Mon, May 5, 2008 at 12:08 PM, hadley wickham <h.wickham_at_gmail.com> wrote:
> On Mon, May 5, 2008 at 10:54 AM, Chip Barnaby <cbarnaby@wrightsoft.com> wrote:
> > Yes, someone else pointed out my typo, sorry.
> >
> > My issue here is that referring to columns by index is risky. Hard-coded
> > indices will be buried in code and there will be trouble if (when) the data
> > organization changes. So I am trying to learn how to work with names, but
> > the language does not seem to allow that approach without some awkwardness.
> > I guess I could write some helper functions.
>
> There's the rename function in the reshape package:
>
> library(reshape)
> X <- rename(X, c("bob" = "sue")

Or if you just want the function:

rename <- function (x, replace) {

    replacement <- replace[names(x)]
    names(x)[!is.na(replacement)] <- replacement[!is.na(replacement)]     x
}

Hadley

-- 
http://had.co.nz/

______________________________________________
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 Mon 05 May 2008 - 17:24:13 GMT

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 Mon 05 May 2008 - 18:30:34 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