[R] Change data frame column names

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jul 15 17:12:32 CEST 2009


On Wed, 2009-07-15 at 14:35 +0000, Tom Liptrot wrote:
> 
> 
> 
> Hi R helpers,
> 
> I have a data frame and I want to change the column names to names I have held in another data frame, but I am having difficulty. All data framnes are large so i can't input manually. Below is what i have tried so far:
> 
> df<-data.frame(a=1:5, b=2:6, d=3:7, e=4:8)
> coltitles<-data.frame(v1="col number one", v2="col number two", v3="col number three", v4="col number four")
> 
> ##first attempt
> 
> names(df)<-coltitles
> names(df)
> [1] "1" "1" "1" "1"   ###not what i wanted as I want names(df) to return [1] "col number one" "col number two" "col number three" "col number four"
> 
> 
> 
> ##second attempt
> 
> coltitles<-as.vector(coltitles, mode="character")  ##trying to convert to a character vector after looking at help
> is.vector(coltitles)
> [1] TRUE
> names(df)<-coltitles
> names(df)
> [1] "1" "1" "1" "1"   ###again not what I wanted
> 
> How can I convert the column names?

This seems a bit of a strange way to go about things, but if needs must:

> names(df) <- unlist(coltitles)
> df
  col number one col number two col number three col number four
1              1              2                3               4
2              2              3                4               5
3              3              4                5               6
4              4              5                6               7
5              5              6                7               8
> names(df)
[1] "col number one"   "col number two"   "col number three" "col number four"

If your data frame with the names in the one (and only) row (why store
this as a one row df and not a character vector??? [1]) then by
unlisting it we get a (or something that can be coerced to) a character
vector of the correct names.

If coltitles contains more rows, then:

names(df) <- unlist(coltitles[1,])

might be more appropriate.

HTH

G

[1] Your df storage of names is soooo inefficient (for this task):
> object.size(names(df)) # after running the above code
312 bytes
> object.size(coltitles)
2728 bytes



> 
> Thanks in advance,
> 
> Tom
> 
> Beyond Hotmail - see what else you can do with Windows Live. Find out more.
> _________________________________________________________________
> 
> 
> 	[[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.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list