Skip to content
Prev 313045 / 398503 Next

Changing data frame column headings

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:

            
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.