How to rename one or more columns of a dataframe

dplyr
dplyr rename()
Learn how to rename one or more columns of a dataframe with this comprehensive R tutorial. Includes practical examples and code snippets.
Published

May 17, 2022

Introduction

Renaming columns is a fundamental data manipulation task that makes your datasets more readable and analysis-ready. The rename() function from dplyr provides a clean, intuitive way to change column names without affecting the underlying data. This approach is essential when working with datasets that have unclear variable names or when you need to standardize naming conventions across multiple dataframes.

Getting Started

library(tidyverse)
library(palmerpenguins)

Example 1: Basic Usage

The Problem

The penguins dataset has column names that could be more descriptive for our analysis. We want to rename a single column to make it clearer what the data represents.

Step 1: Examine the original data

Let’s first look at the current column names in our dataset.

data(penguins)
colnames(penguins)
head(penguins, 3)

This shows us the original structure with column names like “bill_length_mm” that we might want to make more concise.

Step 2: Rename a single column

We’ll rename the “bill_length_mm” column to something shorter and clearer.

penguins_renamed <- penguins |>
  rename(bill_length = bill_length_mm)

colnames(penguins_renamed)

The rename() function uses the syntax new_name = old_name, successfully changing our column name while preserving all data.

Step 3: Verify the change

Let’s confirm that only the column name changed, not the data itself.

head(penguins_renamed |> select(bill_length, bill_depth_mm), 3)

The data remains identical, but now we have a cleaner column name that’s easier to reference in our code.

Example 2: Practical Application

The Problem

You’re preparing a report and need to rename multiple columns in the mtcars dataset to make them more presentation-ready. The current abbreviations like “mpg”, “cyl”, and “disp” need to be expanded to full, descriptive names that stakeholders will understand.

Step 1: Identify columns to rename

First, let’s see what we’re working with in the mtcars dataset.

data(mtcars)
colnames(mtcars)
head(mtcars, 2)

We can see several abbreviated column names that would benefit from more descriptive labels.

Step 2: Rename multiple columns at once

We’ll rename several columns simultaneously using multiple arguments in rename().

mtcars_clean <- mtcars |>
  rename(
    miles_per_gallon = mpg,
    cylinders = cyl,
    displacement = disp
  )

This demonstrates how rename() can handle multiple column changes in a single operation, making your code more efficient.

Step 3: Create a complete transformation

Let’s finish by renaming the remaining important columns for our report.

mtcars_final <- mtcars_clean |>
  rename(
    horsepower = hp,
    weight = wt,
    quarter_mile_time = qsec
  )

colnames(mtcars_final)

Now we have a dataset with fully descriptive column names that are self-explanatory for any reader.

Step 4: Combine with other operations

We can chain rename() with other dplyr functions for a complete data preparation workflow.

report_data <- mtcars_final |>
  select(miles_per_gallon, cylinders, horsepower, weight) |>
  filter(cylinders >= 6) |>
  arrange(desc(miles_per_gallon))

head(report_data)

This shows how renaming fits naturally into a larger data manipulation pipeline, creating clean, analysis-ready data.

Summary

  • Use rename(new_name = old_name) to change column names while preserving data
  • Multiple columns can be renamed in a single rename() call by separating them with commas
  • The rename() function integrates seamlessly with pipe operations and other dplyr functions
  • Always verify your changes by checking column names with colnames() after renaming
  • Descriptive column names improve code readability and make datasets more accessible to stakeholders