[R] names help?

Mike Prager mike.prager at noaa.gov
Wed Dec 31 19:15:26 CET 2008


<rkevinburton at charter.net> wrote:

> I am unclear what names(x) <- does?
> 
> On the outset it seems relatively clear. 
> 
> But what if there are already names?
> 
> What if there are already names on the object and they are in a different order than specified in the names on the right hand side? I tried it and it just replaces what was there as seems the simplest approach. But doesn't that seem a little dangerous? If I swap names for a variable and then later reference the variable by name I would get unexpected results. Right?
> 
> Kevin

Right: when you change a name, the old name no longer works.

You can also do things like this, to change specific names:

-----Begin R session-----
> a = 1:5
> names(a) <- letters[1:5]
> a
a b c d e 
1 2 3 4 5 
> names(a)[3] <- "Kevin"
> a
    a     b Kevin     d     e 
    1     2     3     4     5 
> names(a)[which(names(a)=="b")] <- "Brad"
> a
    a  Brad Kevin     d     e 
    1     2     3     4     5 
-----End R session-----

Does that help?

-- 
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.



More information about the R-help mailing list