Open In App

How to Rename Multiple Columns in R

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Renaming columns in R Programming Language is a basic task when working with data frames, and it’s done to make things clearer. Whether you want names to be more understandable, follow certain rules, or match your analysis, there are different ways to change column names.

There are types of methods available for Rename Multiple Columns in R

  1. Using the names function
  2. Using the colNames function
  3. Using “dplyr” package
  4. Using Index
  5. Using Setnames

R




# Create a sample data frame
df <- data.frame(
  old_name1 = c(1, 2, 3),
  old_name2 = c(4, 5, 6),
  old_name3 = c(7, 8, 9)
)
 
# Display the original data frame
print("Original Data Frame:")
print(df)


Output:

[1] "Original Data Frame:"

old_name1 old_name2 old_name3
1 1 4 7
2 2 5 8
3 3 6 9

Use the names() function to rename the columns

R




# Specify the new column names
new_names <- c("new_name1", "new_name2", "new_name3")
 
# Rename columns using names()
names(df) <- new_names
 
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)


Output:

[1] "Data Frame with Renamed Columns:"

new_name1 new_name2 new_name3
1 1 4 7
2 2 5 8
3 3 6 9

Using colNames() function

R




# Create a sample data frame
df <- data.frame(
  old_Frame1 = c(1, 2, 3),
  old_Frame2 = c(4, 5, 6),
  old_Frame3 = c(7, 8, 9)
)
 
# Display the original data frame
print("Original Data Frame:")
print(df)


Output

[1] "Original Data Frame:"

old_Frame1 old_Frame2 old_Frame3
1 1 4 7
2 2 5 8
3 3 6 9

Now , use the colnames() function for renaming the multiple columns

R




# Specify the new column names
new_names <- c("new_Frame1", "new_Frame2", "new_Frame3")
 
# Rename columns using colnames()
colnames(df) <- new_names
 
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)


Output:

[1] "Data Frame with Renamed Columns:"

new_Frame1 new_Frame2 new_Frame3
1 1 4 7
2 2 5 8
3 3 6 9

Rename Multiple Columns in R Using “dplyr” package

R




# Load the dplyr package
library(dplyr)
 
# Create a sample data frame
df <- data.frame(
  old_Column1 = c(1, 1, 1),
  old_Column2 = c(5, 5, 5),
  old_Column3 = c(8, 8, 8)
)
 
# Display the original data frame
print("Original Data Frame:")
print(df)


Output:

[1] "Original Data Frame:"

old_Column1 old_Column2 old_Column3
1 1 5 8
2 1 5 8
3 1 5 8

Use the dplyr package rename function for renaming multiple columns

R




# Rename columns using dplyr's rename() function
df <- df %>%
  rename(new_Column1 = old_Column1,
         new_Column2 = old_Column2,
         new_Column3 = old_Column3)
 
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)


Output:

[1] "Data Frame with Renamed Columns:"

new_Column1 new_Column2 new_Column3
1 1 5 8
2 1 5 8
3 1 5 8

Rename Multiple Columns in R Using Index

R




# Rename multiple columns by index
 
# Load library
library(dplyr)
 
# Create a sample data frame
my_dataframe <- data.frame(
  old1 = c(1, 2, 3),
  old2 = c(4, 5, 6),
  old3 = c(7, 8, 9)
)
print("Old Dataframe")
print(my_dataframe)


Output

[1] "Old Dataframe"

old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9

Use the Column Index for renaming

R




# Rename columns by index
my_dataframe <- my_dataframe %>%
  rename(c1 = !!1, c2 = !!2)
 
# Print the data frame with renamed columns
print("New Dataframe")
print(my_dataframe)


Output:

[1] "New Dataframe"

c1 c2 old3
1 1 4 7
2 2 5 8
3 3 6 9

Rename Multiple Columns in R Using Setnames ()

R




# Load the data.table package
library(data.table)
 
# Create a sample data.table
my_data_table <- data.table(
  oldName1 = c(1, 2, 3),
  oldName2 = c(4, 5, 6),
  oldName3 = c(7, 8, 9)
)
print("Orginal Dataframe")
print(my_data_table)


Output:

[1] "Orginal Dataframe"

oldName1 oldName2 oldName3
1: 1 4 7
2: 2 5 8
3: 3 6 9

Use the data.table package’s setnames() function for renaming the columns

R




# Specify the new column names
new_names <- c("newName1", "newName2", "newName3")
 
# Rename columns using setnames()
setnames(my_data_table, old = c("oldName1", "oldName2", "oldName3"), new = new_names)
 
# Print the data.table with renamed columns
print("New Dataframe")
print(my_data_table)


Output:

[1] "New Dataframe"

newName1 newName2 newName3
1: 1 4 7
2: 2 5 8
3: 3 6 9

Conclusion

In R, changing column names is easy and can be done in different ways. We can use basic functions like names() and colnames(), the helpful dplyr package with its rename() function, specify column positions, or use setnames() from the data.table package. Each approach caters to different preferences and scenarios, providing users with flexibility in adapting their code to suit diverse data manipulation requirements.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads