[R] columns called X rename Y

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Jan 19 00:13:46 CET 2013


If you have a lot of names to change, or you need to do it regularly, you can use something like:

renames <- read.table(text=
"oldname newname
constant c
numbers b
", header=TRUE, as.is=TRUE )
names(seba)[match(renames$oldname, names(seba))] <- renames$newnames

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Rui Barradas <ruipbarradas at sapo.pt> wrote:

>Hello,
>
>Try the following.
>
>
>names(seba)[grep("numbers", names(seba))] <- "b"
>names(seba)[grep("constant", names(seba))] <- "c"
>
>names(seba)
>
>
>Hope this helps,
>
>Rui Barradas
>
>Em 18-01-2013 18:14, Sebastian Kruk escreveu:
>> I have a data. frame to which you want to change the names to some of
>their
>> columns.
>>
>> For example:
>>
>>> seba <- data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' =
>LETTERS
>> [1:10], otros = 2:11)
>>
>> List their names:
>>
>>> names (Seba)
>> [1] "constant" "numbers" "letters"
>>
>> I want to rename c the column called constant and b the column
>> called numbers.
>>
>> I do not know in which order appear names.
>>
>> How could do it?
>>
>> Thanks in advance,
>>
>> Sebastian.
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
>______________________________________________
>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