Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column name by looking up

Tags:

dataframe

r

I know that I can change a data.frame column name by:

colnames(df)[3] <- "newname"

But there might be cases where the column I want to change is not in the 3rd position. Is there a way to look up the column by name and change it? Like this...

colnames(df)[,"oldname"] <- "newname"

BTW, I have tried this code and I keep getting incorrect number of subscripts on matrix.

Thanks.

like image 303
James Avatar asked Sep 27 '11 20:09

James


1 Answers

colnames(df)[colnames(df)=="oldname"] <- "newname"

or just names

names(df)[names(df)=="oldname"] <- "newname"

There are various functions for renaming columns in packages as well.

like image 133
Brian Diggs Avatar answered Sep 18 '22 13:09

Brian Diggs