[R] columns called X rename Y

David Winsemius dwinsemius at comcast.net
Sat Jan 19 03:22:07 CET 2013


On Jan 18, 2013, at 3:13 PM, Jeff Newmiller wrote:

> 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

Thnks Jeff;

I like it, plan to add it to my import scripts, ... although the 'newname' column is misspelled.

If you have a set of target variable and a diverse set of input variables that need to be mapped to a constant set of names and a re thinking of incorporating in a function, it might be better with:

names(newdat)[ match(renames[['oldname']], names(newdat))] <- renames[['newsource3']]
save(renames, file="name_map_data.rda")

> 
> ---------------------------------------------------------------------------
> 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)

I would think that it might be dangerous if there were multiple column names that contained either "numbers" or "data".


>> 
>> 
>> 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.
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list