R Advanced

Renaming a Single Column of DataFrame in R

When you are working with real-time projects, you often come across raw datasets where columns have names like (“v1”, “var2”, and “cust_id”). These names seem very cryptic and are not descriptive.

That’s why we must rename them to descriptive names (e.g., “age”, “income”, “customer_ID”), which makes the data much easier to understand at a glance.

Here are the four ways to rename a data frame column in R:

  1. Using colnames()
  2. Using names()
  3. Using rename() from the dplyr
  4. Using rename_with() from dplyr

Method 1: Using colnames()

The most efficient way to rename a single column is to use Base R’s colnames() function, which sets or gets the names of the data frame columns. This function provides flexibility, allowing you to rename a column by name or position.

For renaming, you want to assign a new character vector to colnames(your_data_frame). Ensure that the vector length is the same as the number of columns.

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh"),
  score = c(85, 90, 78),
  subject = c("Math", "Math", "History"),
  grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df

colnames(df)[colnames(df) == "grade"] <- "class"
print("After renaming the column")
df

Output

You can see from the above visual representation that we renamed the column from “grade” to “class”.

Pros

  1. The colnames() is a base method. So, it does not require any third-party package.
  2. It works blazing for simply renaming.

Cons

  1. It becomes cumbersome while renaming multiple columns. It’s not so efficient in that scenario.
  2. It relies on the order of the columns. So, if the order changes, you might see unexpected results.

Method 2: Using names()

The names() is a general-purpose base function that can be used to get or set names of various R objects, including data frame columns.

How can I use it? Well, assign a new character vector (column name) to names(your_data_frame). It will be renamed with your new character vector.

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh"),
  score = c(85, 90, 78),
  subject = c("Math", "Math", "History"),
  grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df

names(df)[names(df) == "grade"] <- "class"
print("After renaming the column")
df

Output

As illustrated in the above image, our final data frame’s column name has been changed.

The pros and cons are the same as the “colnames()” method.

Method 3: Using rename() function from dplyr package

You can also use the dplyr::rename() function, where you specify the new column name on the left side of the = and the old name on the right side.

library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh"),
  score = c(85, 90, 78),
  subject = c("Math", "Math", "History"),
  grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df

print("After renaming the column")
df %>% rename(class = grade)

Output

You can see from the above figure that dplyr provides a concise syntax for renaming single or multiple columns of the data frame.

Pros

  1. It is a perfect way to rename even multiple columns as well. It is fast and efficient.
  2. It provides an easier syntax that is concise and easily understandable.

Cons

  1. We need to install a separate package, “dplyr”, for this approach.

Method 4: Using rename_with() from dplyr package

If you want to rename single or multiple columns at once, you can use the “rename_with()” function from the “dplyr” package.

This approach is not only helpful for changing the column name but also for converting column names to lowercase or replacing specific characters.

 library(dplyr)

df <- data.frame(
  name = c("Krunal", "Ankit", "Rushabh"),
  score = c(85, 90, 78),
  subject = c("Math", "Math", "History"),
  grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df

print("After renaming the column")
df <- df %>% rename_with(~ ifelse(. == "grade", "class", .), .cols = "grade")
df

Output

Pros

  1. It provides a transforming function through which you can perform any operation, including changing the column name. Thus, it is a multi-functional approach.
  2. It is also efficient for renaming multiple columns at once.

Cons

  1. You must install the “dplyr” package in your R environment.
  2. This approach might be an overkill if you just want to rename a column.

Summary

For renaming a single column, use the “colnames()” or “names()” function.

For renaming multiple columns at once in R, use the “rename_with()” or “rename()” function from the “dplyr” package.

Recent Posts

cbind() Function: Binding R Objects by Columns

R cbind (column bind) is a function that combines specified vectors, matrices, or data frames…

1 week ago

rbind() Function: Binding Rows in R

The rbind() function combines R objects, such as vectors, matrices, or data frames, by rows.…

2 weeks ago

as.numeric(): Converting to Numeric Values in R

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I…

3 weeks ago

Calculating Natural Log using log() Function in R

The log() function calculates the natural logarithm (base e) of a numeric vector. By default,…

4 weeks ago

Dollar Sign ($ Operator) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of…

1 month ago

Calculating Absolute Value using abs() Function in R

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only…

2 months ago