[R] Column renaming

hadley wickham h.wickham at gmail.com
Mon May 5 19:09:12 CEST 2008


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 at 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/



More information about the R-help mailing list