How to change column header names in R DataFrames?

After acquiring your data into your R dataframe, you might probably you will need to tweak your variables (columns) naming convention. In this tutorial we will learn how to rename R DataFrame columns by leveraging the base capabilities of R and also look into dplyr features for easily renaming columns.

Create R DataFrame

We’ll get started by creating an R data frame – feel free to use it to follow along with this tutorial examples:

#R
# define vectors
var1 <- c(14, 15, 16, 23,33)
var2 <- c (342, 112,111,566,232)
var3 <- c (floor(var2/var1))

# create DataFrames
my_df <- data.frame (var1 = var1, var2 = var2, var3 = var3)

Retrieve columns names

We can easily get our DataFrame column names by running the following snippet in RStudio or Jupyter.

print (names(my_df))

# This will return:
[1] "var1" "var2" "var3"

Rename all DataFrame columns

We can rename our DataFrame columns using the following code:

names(my_df) <- c('col1', 'col2', 'col3')

Alternatively, we can use the colnames function and assign the new names:

colnames(my_df) <- c('col1', 'col2', 'col3')

This effectively modifies the variable names:

print (names(my_df))

#Returns the following:
[1] "col1" "col2" "col3"

Change a single column label

We can rename a single column obviously. This code modifies the label of the third column to be col33.

names(my_df)[3] <- 'col33'

Replace column headers with list

We can also use an R list to store the new column names and assign those as header labels.

new_names_lst <- list('col1', 'col2', 'col3')

#assign new names from list
names(my_df) <- c(new_names_lst)

Change columns names with dplyr

We can use the dplyr rename function as well. We can define one, multiple or all DataFrame headers to be modified.

# reset the DataFrame columns
my_df <- data.frame (var1 = var1, var2 = var2, var3 = var3)

library (dplyr)
my_df %>% rename( col1 = var1, col2 = var2, col3=var3)