Rename one named column in R

dataframe, r

Solution

As of October 2014 this can now be done easily in the dplyr package:

rename(data, d = b)

Problem

I want to update one column of a dataframe, referencing it using its original name, is this possible? For example say I had the table 'data' ``` a b c 1 2 2 3 2 3 4 1 2 ``` and I wanted to update the name of column b to 'd'. I know I could use ``` colnames(data)[2] <- 'd' ``` but can I make the change by specifically referencing b, i.e. something like ``` colnames(data)['b'] <- 'd' ``` so that if the column ordering of the dataframe changes the correct column name will still be updated. Thanks in advance

Original source