How to rename a single column in a data.frame?

This is a generalized way in which you do not have to remember the exact location of the variable:

# df = dataframe
# = The name you don't like anymore
# = The name you want to get

names(df)[names(df) == ''] <- ''

This code pretty much does the following:

  1. names(df) looks into all the names in the df
  2. [names(df) ==] extracts the variable name you want to check
  3. <- '' assigns the new variable name.

Leave a Comment