names help?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

names help?

rkevinburton
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: names help?

Mike Prager
<[hidden email]> 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.

______________________________________________
[hidden email] 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.