How to Rename Multiple Columns in R (original) (raw)

Last Updated : 21 Feb, 2026

Renaming columns means changing the existing column names to more meaningful or consistent ones. We often do this to make our data easier to understand, follow naming rules or match analysis requirements. In R, you can rename multiple columns using base R functions or packages like dplyr.

Using names()

We use names() to assign a new vector of names to all columns at once.

df <- data.frame( old_name1 = c(1, 2, 3), old_name2 = c(4, 5, 6), old_name3 = c(7, 8, 9) ) cat("Original Dataframe\n") df

cat("\nRenaming the columns\n") new_names <- c("new_name1", "new_name2", "new_name3") names(df) <- new_names

cat("\nModified Dataframe\n") df

`

**Output:

dataframe

Output

Using colnames()

We use colnames() to change all column names similarly to names().

df <- data.frame( old_Frame1 = c(1, 2, 3), old_Frame2 = c(4, 5, 6), old_Frame3 = c(7, 8, 9) ) cat("Original Dataframe\n") df

cat("\nRenaming the columns\n") new_names <- c("new_Frame1", "new_Frame2", "new_Frame3") colnames(df) <- new_names

cat("\nModified Dataframe\n") df

`

**Output:

dataframe

Output

Using dplyr

We use rename() from the dplyr package to rename specific columns using the format new = old.

library(dplyr)

df <- data.frame( old_Column1 = c(1, 1, 1), old_Column2 = c(5, 5, 5), old_Column3 = c(8, 8, 8) ) cat("Original Dataframe\n") df

cat("\nRenaming the columns\n") df <- df %>% rename(new_Column1 = old_Column1, new_Column2 = old_Column2, new_Column3 = old_Column3)

cat("\nModified Dataframe\n") df

`

**Output:

dataframe

Output

Using Column Index in dplyr

We use column positions with !! and rename() to rename specific columns by index.

library(dplyr)

my_dataframe <- data.frame( old1 = c(1, 2, 3), old2 = c(4, 5, 6), old3 = c(7, 8, 9) ) cat("Original Dataframe\n") my_dataframe

cat("\nRenaming the columns\n") my_dataframe <- my_dataframe %>% rename(c1 = !!1, c2 = !!2)

cat("\nModified Dataframe\n") my_dataframe

`

**Output:

Screenshot-2025-07-11-120808

Output

Using setnames()

We use setnames() from the data.table package to rename multiple columns in place.

library(data.table)

my_data_table <- data.table( oldName1 = c(1, 2, 3), oldName2 = c(4, 5, 6), oldName3 = c(7, 8, 9) ) cat("Original Dataframe\n") my_data_table

cat("\nRenaming the columns\n") new_names <- c("newName1", "newName2", "newName3") setnames(my_data_table, old = c("oldName1", "oldName2", "oldName3"), new = new_names)

cat("\nModified Dataframe\n") my_data_table

`

**Output:

dataframe

Output

The column names are changed from oldName1, oldName2, oldName3 to newName1, newName2, newName3, while the data remains unchanged. The output confirms the renaming in a data.table format.