Rename columns in R

rename-columns-in-r-studio

In this guide, we will learn different ways to rename columns in R Dataframe using simple steps, including using the popular dplyr package.

When you’re working with data in R, you often need to give your columns new names. This helps you keep things neat and organized, making your data analysis smoother.

Learn R Programming free from Coursera: R Programming – Coursera

Basic Column Renaming

Let’s start with the simplest way of renaming columns. Imagine you have a table named data and we need to change column names for that. We can simply do this using the names() function:

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Print input data frame
print("Input Data")
print(data)

# Rename columns using names() function
names(data)[names(data) == "old_name1"] <- "new_name1"
names(data)[names(data) == "old_name2"] <- "new_name2"

# Print output dataframe
print("Output Data")
print(data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6

> # Print output dataframe
...
...
> print("Output Data")
[1] "Output Data"
> print(data)
  new_name1 new_name2
1         1         4
2         2         5
3         3         6

This method renames columns one by one. The only issue with this method is that it’s a bit slow. When you have lots of columns to rename it may take some time.

Renaming Columns Using dplyr

The dplyr package provides an easy and elegant way to rename columns in R. Let’s see how we can do this:

Renaming One Column

To rename a single column in a table, we can use the rename() function of dplyr package:

library(dplyr)

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename a single column
new_data <- data %>%
  rename(new_name1 = old_name1)

# Print input data frame
print("Input Data")
print(data)

# Print output dataframe
print("Output Data")
print(new_data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6
> 
> # Print output dataframe
> print("Output Data")
[1] "Output Data"
> print(new_data)
  new_name1 old_name2
1         1         4
2         2         5
3         3         6

As you can see in the above output, only name of the first column (old_name1 -> new_name1) got changed. The second column name (old_name2) remains same. As we only renamed the first column.

Also Read:  Learn Web Scraping using Beautifulsoup and Python

Renaming Multiple Columns

If you want to rename more than one column, you can chain rename() functions together:

library(dplyr)

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename multiple columns
new_data <- data %>%
  rename(new_name1 = old_name1,
         new_name2 = old_name2)

# Print input data frame
print("Input Data")
print(data)

# Print output dataframe
print("Output Data")
print(new_data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6
> 
> # Print output dataframe
> print("Output Data")
[1] "Output Data"
> print(new_data)
  new_name1 new_name2
1         1         4
2         2         5
3         3         6

Or, use the rename_with() function along with across() to rename multiple columns based on a condition:

library(dplyr)

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename columns using rename_with() and across()
new_data <- data %>%
  rename_with(~ paste0("new_", .), starts_with("old_"), .names = "to_replace")

# Print input data frame
print("Input Data")
print(data)

# Print output dataframe
print("Output Data")
print(new_data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6
> 
> # Print output dataframe
> print("Output Data")
[1] "Output Data"
> print(new_data)
  new_old_name1 new_old_name2
1             1             4
2             2             5
3             3             6

Renaming Columns by Index, Name, or Position

Now let’s see how we can rename column in R by index, name or by position.

Renaming by Index

To rename column in R by index, you can use rename_at() function of dplyr package.

library(dplyr)

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename column by index in R
new_data <- data %>%
  rename_at(1, ~ "new_name")

# Print input data frame
print("Input Data")
print(data)

# Print output dataframe
print("Output Data")
print(new_data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6
> 
> # Print output dataframe
> print("Output Data")
[1] "Output Data"
> print(new_data)
  new_name old_name2
1        1         4
2        2         5
3        3         6

Here in the above R code, the line new_data <- data %>% rename_at(1, ~ "new_name") renames the column at index 1 (old_name1) to "new_name". The tilde (~) is used to specify the new column name.

Also Read:  Subset Pandas Dataframe by Column Value

Renaming by Name

Rename a column by its name is also easy using rename_at() in R.

In the below code, we use the %>% pipe operator to chain the rename_at() function, which renames the column "old_name1" to "new_name".

library(dplyr)

# Create a sample table
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename column by name in R
new_data <- data %>%
  rename_at("old_name1", ~ "new_name")

# Print input data frame
print("Input Data")
print(data)

# Print output dataframe
print("Output Data")
print(new_data)
> # Print input data frame
> print("Input Data")
[1] "Input Data"
> print(data)
  old_name1 old_name2
1         1         4
2         2         5
3         3         6
> 
> # Print output dataframe
> print("Output Data")
[1] "Output Data"
> print(new_data)
  new_name old_name2
1        1         4
2        2         5
3        3         6

Renaming by Position

To rename a column by its position, you can use the rename() function in R with indexing:

# Load the dplyr package
library(dplyr)

# Create a sample dataframe
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Get the column names
column_names <- names(data)

# Define the new name
new_name <- "new_column_name"

# Update the column name by position
column_names[1] <- new_name

# Set the updated column names to the dataframe
names(data) <- column_names

# Print the updated dataframe
print(data)
> # Print the updated dataframe
> print(data)
  new_column_name old_name2
1               1         4
2               2         5
3               3         6

In this code, we retrieve the column names using names(data), and then we modify the name at the desired position (e.g. index 1: column_names[1]) to the new name.

Finally, we set the modified column names back to the dataframe using names(data) <- column_names.

Renaming Columns in a Loop

If you want to rename columns using a loop in R language, the following R code is for you.

In this code, we first create a list called column_mapping where you can specify the old column names as keys and their corresponding new column names as values. Then, we define a sample dataframe named data with columns that need renaming.

Also Read:  Calculate Age from Date of Birth using Excel

The loop iterates through the names in column_mapping, and for each old column name, it retrieves the corresponding new column name and renames the column in the data dataframe using the rename() function from the dplyr package.

# List of old and new column names
column_mapping <- list(old_name1 = "new_name1",
                       old_name2 = "new_name2")

# Sample dataframe
data <- data.frame(old_name1 = c(1, 2, 3),
                   old_name2 = c(4, 5, 6))

# Rename columns in a loop
for (old_name in names(column_mapping)) {
  new_name <- column_mapping[[old_name]]
  data <- data %>%
    dplyr::rename(!!new_name := !!old_name)
}

# Print the updated dataframe
print(data)
> # Print the updated dataframe
> print(data)
  new_name1 new_name2
1         1         4
2         2         5
3         3         6

Wrapping Up

That’s all to rename columns in R. In this article, I tried to cover the basics of renaming column name in R dataframe and learned how to use the dplyr package to make renaming columns a breeze.

Whether you’re giving a column a new name or revamping the entire table, these techniques will help you keep your data tidy and your analysis to the point.

If you want to learn R Programming free from Coursera, then you can checkout this course: R Programming – Coursera

Leave a comment