[R] Renaming variables

Dennis Murphy djmuser at gmail.com
Tue Jun 14 16:01:39 CEST 2011


Hi:

names() typically has to do with the names associated with
*components* of a data frame or list, which appears to be what you
have, reading between the lines. Here's a toy example:

> mydf <- data.frame(a = rnorm(5))
> mydf
             a
1  1.291952883
2 -0.136706733
3  0.219528682
4 -0.174832617
5 -0.003101679
> names(mydf) <- 'b'
## b is a variable name within the data frame mydf, not the global environment:
> b
Error: object 'b' not found
> mydf$b     ## or equivalentally/preferably,   mydf[['b']]
[1]  1.291952883 -0.136706733  0.219528682 -0.174832617 -0.003101679
> mydf
             b
1  1.291952883
2 -0.136706733
3  0.219528682
...

# OTOH,
> b <- mydf
> b
             b
1  1.291952883
2 -0.136706733
3  0.219528682
...

so by assigning mydf to an object named b, you can change the name of
mydf. Note, however, that mydf still exists; b is a copy of mydf.
rm(mydf) would remove the original in favor of the newly named copy.


HTH,
Dennis

On Tue, Jun 14, 2011 at 4:04 AM, mosojn <marta.osojnik at gmail.com> wrote:
> Hi guys,
>
> I checked previous posts and I saw similar questions have been answered;
> they didn't help me solve my problem though. I am using R version 2.13.0
> (2011-04-13), Platform: x86_64-pc-mingw32/x64 (64-bit) and I am having
> difficulties renaming variables. I tried with this command:
>
> names (oldvariable) <- 'new_variable'
>
> and now when I type in oldvariable the new name pops out in form: [1]
> "new_variable" BUT when I try typing in new_variable I get the message
>
> Error: object 'new_variable' not found
>
> I also tried with fix(mydata) command, but editor basically opens up empty,
> so I don't understand why this is happening.
>
> If you know how to advise me, I'll be grateful.
>
> Thank you!
>
> Marta
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Renaming-variables-tp3596227p3596227.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list