A quick question please! How do you rename column names? i.e. V1 --> Apple; V2 --> Orange, etc. thx much ej [[alternative HTML version deleted]]
names(data) <- c('Apple', 'Orange')> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ethan Johnsons > Sent: Monday, September 11, 2006 12:49 PM > To: r-help at stat.math.ethz.ch > Subject: [R] rename cols > > A quick question please! > > How do you rename column names? i.e. V1 --> Apple; V2 --> > Orange, etc. > > thx much > > ej > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Ethan Johnsons <ethan.johnsons <at> gmail.com> writes:> > A quick question please! > > How do you rename column names? i.e. V1 --> Apple; V2 --> Orange, etc.There are some nice utilities in Frank Harrell's Hmisc package. See: http://lib.stat.cmu.edu/S/Harrell/help/Hmisc/html/upData.html Also look at his Design library.
> How do you rename column names? i.e. V1 --> Apple; V2 --> Orange, etc.I'm fairly new myself, but... People will tell you: 1. See if just asking for help on the verb you want works ?rename #out of luck, never mind 2. If not, try some variations ?name #too bad: turns out this doesn't help here 3. If not, then search more widely RSiteSearch("rename") # sadly that's too vague to help 4. If not, then ask here, giving a clear and concise example of your needs, data, and expected result. i.e.,I have A <- c(V1=2, V2=3) How can I change the variable names? But for really simple questions like this (which are more like the syntax of the language than anything more complex, you will not regret buying "An Introduction to R", and something like "Statistics an introduction using R", both on Amazon for quick delivery. The answer is that the function to get names also sets them if it is passed an array of names, so: A <- c(V1=2, V2=3) names(A) [1] "V1" "V2" names(A) <- c("Apple", "Orange") A Apple Orange 2 3
The following works for data frames and matrices (you didn't say which you were working with). > x <- data.frame(V1=1:3,V2=4:6) > x V1 V2 1 1 4 2 2 5 3 3 6 > colnames(x) <- c("Apple", "Orange") > x Apple Orange 1 1 4 2 2 5 3 3 6 > For a data frame, 'names(x) <- c("Apple", "Orange")' also works, because a dataframe is stored internally as a list of columns. -- Tony Plate Ethan Johnsons wrote:> A quick question please! > > How do you rename column names? i.e. V1 --> Apple; V2 --> Orange, etc. > > thx much > > ej > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
For a newcomer who wants to rename variable "fksm" and "klmk" in a dataframe of with 439 variables there is not easy and intuitive solution. That person has to spend a lot of time listing columns and counting columns or doing string searches or using brackets within brackets within brackets to get a simple thing done. Is there a simple function or solution to this in R without using an add-on package?
I don't know, this seems pretty simple and intuitive (to me) # Create a sample data set with 439 variables tmp <- data.frame(matrix(c(rnorm(4390)), ncol=439)) colnames(tmp)<-paste("col", 1:439, sep = "") # rename a certain variable in that dataset names(tmp)[(which(names(tmp)=='col1'))]<-'NewName' Here you are using indexing and a simple evaluation to find a variable by name in a large data set and rename it. R is so flexible that this cat can be skinned in many (and maybe even easier) ways, though. Harold> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Anupam Tyagi > Sent: Tuesday, September 12, 2006 12:34 AM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] rename cols > > For a newcomer who wants to rename variable "fksm" and "klmk" > in a dataframe of with 439 variables there is not easy and > intuitive solution. That person has to spend a lot of time > listing columns and counting columns or doing string searches > or using brackets within brackets within brackets to get a > simple thing done. Is there a simple function or solution to > this in R without using an add-on package? > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Hi On 12 Sep 2006 at 6:44, Anupam Tyagi wrote: To: r-help at stat.math.ethz.ch From: Anupam Tyagi <AnupTyagi at yahoo.com> Date sent: Tue, 12 Sep 2006 06:44:03 +0000 (UTC) Subject: Re: [R] rename cols> Christos Hatzis <christos <at> nuverabio.com> writes: > > > > > Try this: > > > > old.colnames <- colnames(my.439.vars.df) > > old.colnames[old.colnames=="fksm"] <- "new.name.a" > > old.colnames[old.colnames=="klmk"] <- "new.name.b" > > For a newcomer, it will be useful to have a function like this in the > base R: that can take a list of old.names and new.names, and do the > assignment. It is far more efficient to have functions that are shared > via the R distribution, than having to write own functions for > carrying out basic data management tasks, and simple routinely used > statistical procedures. Most users would rather spend time on thinking > about the substantive work, instead of figuring out how to > program---this may be specially true for new users. This way theR is programming environment and language so more or less even newcomers are expected to do some programming. And it is also volunteer project. I expect that renaming columns is task which is not done very often, so there did not come up anybody who was interested in programming such function. Also the pool of available functions seems to be quite extensive in base R and even bigger in all available packages. Sometimes is hard enough to remember correct function name. There is also excellent help and manual pages with nice copy/paste feature examples. I wonder if you ever tried to get some help from e.g. Excel help. Quite often you are completely lost.> functions used will also be more efficient and better designed than > the typical new user.If a new user does not wont to use command line syntax he/she can use some of available GUIS (see R GUI in home page) e.g. JGR. Petr> > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz
> For a newcomer who wants to rename variable "fksm" and "klmk" in a dataframe of > with 439 variables there is not easy and intuitive solution. That person has to > spend a lot of time listing columns and counting columns or doing string > searches or using brackets within brackets within brackets to get a simple thing > done. Is there a simple function or solution to this in R without using an > add-on package?I use: rename <- function(x, replace) { replacement <- replace[names(x)] names(x)[!is.na(replacement)] <- replacement[!is.na(replacement)] x } (which is available in the reshape package) You use it like: df <- data.frame(a=1:2, b=3:4) df <- rename(df, c(a="variable 1")) Hadley
Hi There is even an Excel like possibility for renaming columns. try newDF<-edit(oldDF) you can go through columns and after clicking on header you can change column name. Petr On 12 Sep 2006 at 8:31, hadley wickham wrote: Date sent: Tue, 12 Sep 2006 08:31:43 -0400 From: "hadley wickham" <h.wickham at gmail.com> To: "Anupam Tyagi" <AnupTyagi at yahoo.com> Copies to: r-help at stat.math.ethz.ch Subject: Re: [R] rename cols> > For a newcomer who wants to rename variable "fksm" and "klmk" in a > > dataframe of with 439 variables there is not easy and intuitive > > solution. That person has to spend a lot of time listing columns and > > counting columns or doing string searches or using brackets within > > brackets within brackets to get a simple thing done. Is there a > > simple function or solution to this in R without using an add-on > > package? > > I use: > > rename <- function(x, replace) { > replacement <- replace[names(x)] > names(x)[!is.na(replacement)] <- replacement[!is.na(replacement)] > x > } > > (which is available in the reshape package) > > You use it like: > > df <- data.frame(a=1:2, b=3:4) > df <- rename(df, c(a="variable 1")) > > Hadley > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Petr Pikal petr.pikal at precheza.cz