I have a reshaped data frame with value column headings concatenated from two column headings in the melted data frame. I want to change all 56 headings in a single command, but 'names' allows me to change only one at a time. In Hadley's 2007 article on reshape in the Journal of Statistical Software he mentions a 'rename' function, but I cannot find that. Is there a way to change all data frame column headings in a single command? Rich
Changing data frame column headings
11 messages · arun, R. Michael Weylandt, Rich Shepard +1 more
x <- data.frame(a = 1:5, b = rnorm(5)) names(x) <- LETTERS[2:1] print(x) seems to work. Can you be more explicit about your problem? Michael
On Wed, Dec 5, 2012 at 6:51 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
I have a reshaped data frame with value column headings concatenated from two column headings in the melted data frame. I want to change all 56 headings in a single command, but 'names' allows me to change only one at a time. In Hadley's 2007 article on reshape in the Journal of Statistical Software he mentions a 'rename' function, but I cannot find that. Is there a way to change all data frame column headings in a single command? Rich
______________________________________________ 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.
Hi, I am not sure why ?rename() is not working. ?a <- list(a = 1, b = 2, c = 3) ?? ? rename(a, c(b = "a", c = "b", a="c")) A.K. ----- Original Message ----- From: Rich Shepard <rshepard at appl-ecosys.com> To: r-help at r-project.org Cc: Sent: Wednesday, December 5, 2012 1:51 PM Subject: [R] Changing data frame column headings ? I have a reshaped data frame with value column headings concatenated from two column headings in the melted data frame. I want to change all 56 headings in a single command, but 'names' allows me to change only one at a time. In Hadley's 2007 article on reshape in the Journal of Statistical Software he mentions a 'rename' function, but I cannot find that. ? Is there a way to change all data frame column headings in a single command? Rich ______________________________________________ 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.
On Wed, 5 Dec 2012, R. Michael Weylandt wrote:
Can you be more explicit about your problem?
Michael, Data frame contains water chemistry data; site, date, parameter, value. The column names after dcast() are, for example, alk_quant, ph_quant, tds_quant. I wanted to remove the '_quant' from each column header. Using names() with a string vector of the new names did not work, so I specified each one and names() made the changes. Rich
On Wed, 5 Dec 2012, arun wrote:
I am not sure why ?rename() is not working. ?a <- list(a = 1, b = 2, c = 3) ?? ? rename(a, c(b = "a", c = "b", a="c"))
I have the reshape2 library loaded and ?rename did not find the help page. Are the parentheses required in the command? Thanks, Rich
Hi Rich, You can get ?rename() by either loading library(reshape) or library(plyr). A.K. ----- Original Message ----- From: Rich Shepard <rshepard at appl-ecosys.com> To: R help <r-help at r-project.org> Cc: Sent: Wednesday, December 5, 2012 4:24 PM Subject: Re: [R] Changing data frame column headings
On Wed, 5 Dec 2012, arun wrote:
I am not sure why ?rename() is not working. ?a <- list(a = 1, b = 2, c = 3) ?? ? rename(a, c(b = "a", c = "b", a="c"))
? I have the reshape2 library loaded and ?rename did not find the help page. Are the parentheses required in the command? Thanks, Rich ______________________________________________ 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.
On Wed, Dec 5, 2012 at 9:23 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
On Wed, 5 Dec 2012, R. Michael Weylandt wrote:
Can you be more explicit about your problem?
Michael, Data frame contains water chemistry data; site, date, parameter, value. The column names after dcast() are, for example, alk_quant, ph_quant, tds_quant. I wanted to remove the '_quant' from each column header. Using names() with a string vector of the new names did not work, so I specified each one and names() made the changes. Rich
I'm afraid I'm still having trouble visualizing -- perhaps you could work up a reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Michael
On Wed, 5 Dec 2012, arun wrote:
You can get ?rename() by either loading library(reshape) or library(plyr).
A.K. I wondered about that, but assumed that reshape functions would also be found in reshape2. I now know that's not the case. :-) Much appreciated, Rich
Hi,
This could be done using ?gsub()
set.seed(5)
dat1<-data.frame(alk_quant=sample(1:15,6,replace=TRUE),ph_quant=sample(5:9,6,replace=TRUE),tds_quant=sample(10:20,6,replace=TRUE))
?names(dat1)<-gsub("(.*)\\_.*","\\1",names(dat1))
?head(dat1,2)
#? alk ph tds
#1?? 4? 7? 13
#2? 11? 9? 16
A.K.
----- Original Message -----
From: Rich Shepard <rshepard at appl-ecosys.com>
To: r-help at r-project.org
Cc:
Sent: Wednesday, December 5, 2012 4:23 PM
Subject: Re: [R] Changing data frame column headings
On Wed, 5 Dec 2012, R. Michael Weylandt wrote:
Can you be more explicit about your problem?
Michael, ? Data frame contains water chemistry data; site, date, parameter, value. The column names after dcast() are, for example, alk_quant, ph_quant, tds_quant. I wanted to remove the '_quant' from each column header. ? Using names() with a string vector of the new names did not work, so I specified each one and names() made the changes. Rich ______________________________________________ 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.
If you check, the help files, you will find rename in reshape and plyr, but not reshape2. But you have never shown us the command you used with names() and what didn't work:
a <- data.frame(alk_quant=rnorm(5, 5), ph_quant= rnorm(5, 5),
+ tds_quant=rnorm(5, 5))
a
alk_quant ph_quant tds_quant 1 6.569293 5.494560 5.521039 2 4.854873 5.612902 5.235817 3 4.636218 5.116499 5.973769 4 5.430009 6.273394 3.511017 5 5.714755 6.876349 4.035907
names(a) <- gsub("_quant", "", names(a))
a
alk ph tds 1 6.569293 5.494560 5.521039 2 4.854873 5.612902 5.235817 3 4.636218 5.116499 5.973769 4 5.430009 6.273394 3.511017 5 5.714755 6.876349 4.035907 ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Rich Shepard Sent: Wednesday, December 05, 2012 3:25 PM To: R help Subject: Re: [R] Changing data frame column headings On Wed, 5 Dec 2012, arun wrote:
I am not sure why ?rename() is not working. ?a <- list(a = 1, b = 2, c = 3) ?? ? rename(a, c(b = "a", c = "b", a="c"))
I have the reshape2 library loaded and ?rename did not find the help page. Are the parentheses required in the command? Thanks, Rich
______________________________________________ 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.
On Wed, 5 Dec 2012, David L Carlson wrote:
names(a) <- gsub("_quant", "", names(a))
a
David, I did not pick that up from the names() help page. Thanks for the insight. Rich