I'm coming out with all the classic problems today ;-) Can anyone tell me how to change the column names of an already existing data frame? I've read the docs for ?data.frame and ?as.data.frame but can't figure it out. I want to make a new data.frame from some of the columns of an existing data.frame, but when I do that, the new data.frame columns have the same names as the old data.frame's columns, and I want them to be different.... Sorry for the lame question....
?names data frames have names, rather column names (they are lists with extra structure). On Thu, 26 Aug 2004, michael watson (IAH-C) wrote:> I'm coming out with all the classic problems today ;-) > > Can anyone tell me how to change the column names of an already existing > data frame? I've read the docs for ?data.frame and ?as.data.frame but > can't figure it out. > > I want to make a new data.frame from some of the columns of an existing > data.frame, but when I do that, the new data.frame columns have the same > names as the old data.frame's columns, and I want them to be > different.... > > Sorry for the lame question....-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Michael, See ?colnames. Sean On Aug 26, 2004, at 5:31 AM, michael watson (IAH-C) wrote:> I'm coming out with all the classic problems today ;-) > > Can anyone tell me how to change the column names of an already > existing > data frame? I've read the docs for ?data.frame and ?as.data.frame but > can't figure it out. > > I want to make a new data.frame from some of the columns of an existing > data.frame, but when I do that, the new data.frame columns have the > same > names as the old data.frame's columns, and I want them to be > different.... > > Sorry for the lame question.... > > ______________________________________________ > 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
On Thursday 26 August 2004 11:31, michael watson (IAH-C) wrote:> I'm coming out with all the classic problems today ;-) > > Can anyone tell me how to change the column names of an already existing > data frame? I've read the docs for ?data.frame and ?as.data.frame but > can't figure it out.This is a sample data frame:> myData <- data.frame( col1 = 1:3, col2 = 2:4, col3 = 3:5 ) > myDatacol1 col2 col3 1 1 2 3 2 2 3 4 3 3 4 5 You can change all names by:> names( myData )<- c( "newcol1", "newcol2", "newcol3" ) > myDatanewcol1 newcol2 newcol3 1 1 2 3 2 2 3 4 3 3 4 5 Or a single name by:> names( myData )[ 2 ] <- "newcol2" > myDatacol1 newcol2 col3 1 1 2 3 2 2 3 4 3 3 4 5 Or if you know the name, but not the column number:> names( myData )[ which( names( myData ) == "newcol2" ) ] <- "verynewcol2" > myDatacol1 verynewcol2 col3 1 1 2 3 2 2 3 4 3 3 4 5 All the best, Arne> I want to make a new data.frame from some of the columns of an existing > data.frame, but when I do that, the new data.frame columns have the same > names as the old data.frame's columns, and I want them to be > different.... > > Sorry for the lame question.... > > ______________________________________________ > 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-- Arne Henningsen Department of Agricultural Economics University of Kiel Olshausenstr. 40 D-24098 Kiel (Germany) Tel: +49-431-880 4445 Fax: +49-431-880 1397 ahenningsen at agric-econ.uni-kiel.de http://www.uni-kiel.de/agrarpol/ahenningsen/
Hello,> Can anyone tell me how to change the column names of an already existing > data frame? I've read the docs for ?data.frame and ?as.data.frame but > can't figure it out.names(object.name)=c("col.name1", "col.name2", "col.name3", etc.) # note that the number of names as to the the same as the number of columns in your data frame # Another alternative would be: labels(object.name)[[2]]> I want to make a new data.frame from some of the columns of an existing > data.frame, but when I do that, the new data.frame columns have the same > names as the old data.frame's columns, and I want them to be > different....# For exaple, if you want to create a new data frame called new.dataframe, with columns 1 and 2 from the dataframe1, and # colnm 3 and 10 from dataframe2, you do like this: new.dataframe=data.frame(data.frame1[,1:2], data.frame1[,c(3,10)]) # If you want to call a different name to the col, for example: new.dataframe=data.frame("new name"=data.frame1[,1], "new name2" data.frame2[,3]) # or, once you created you rename the names, as: names(object.name)=c("col.name1", "col.name2", "col.name3", etc.) the [row numbers, col numbers] is for subsetting the dataframes (or whatever objects). hope this helps, All the best Marta Rufino