[R] Column names of a data frame
Marta Rufino
mrufino at ipimar.ualg.pt
Thu Aug 26 12:25:47 CEST 2004
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
More information about the R-help
mailing list