Rename the column name in R using Dplyr

Rename the column name in R can be accomplished by using Dplyr. Dplyr package in R is provided with rename() function which renames the column name or column variable. columns can be renamed using the family of of rename() functions like rename_if(), rename_at() and rename_all(), which can be used for different criteria. These functions serves different purposes ranging from renaming a single column, renaming multiple column at once, rename based on a pattern matching and also based on some specific condition.

  • rename() – rename the old column name with a new name.
  • Rename multiple column at once using rename() function.
  • rename a column name like/starts with using rename_at() function
  • rename a column name on a specific condition using rename_if() function in R.
  • rename by replacing a specific pattern using rename_all() function in R

Rename column name in R 11

 

syntax of the rename() function:

rename(new_name = old_name)
  • new_name = new column name
  • old_name = old column name

We will be using Orange data to depict the rename of column variable.

dplyr rename column name in R 12

 

 

Rename the column name in R using rename() function :

Rename the column name using rename function in dplyr. rename() function takes dataframe as argument followed by new_name = old_name.

library(dplyr)

# Rename the column name of the dataframe

Orange_df = rename(Orange, Life_of_tree = age)
Orange_df

Rename the column name “age” with “Life_of_tree”.

dplyr rename column name in R 13

 

Rename Multiple column at once using rename() function:

Renaming the multiple columns at once can be accomplished using rename() function. rename() function takes dataframe as argument followed by new_name = old_name. we will be passing the column names to be replaced in a vector as shown below.

library(dplyr)

# Rename multiple columns of the dataframe at once

Orange_df <- rename(Orange, c(Tree_type = Tree, Tree_life = age , Tree_circumcirle =circumference ))
Orange_df

we will Rename the all the column name at once so the resultant renamed dataframe will be

dplyr rename column name in R 17

 

 

 

Rename the column name in R using Dplyr pipe operator (%>%)  :

Rename the column name using rename function along with pipe operator in dplyr. rename() function takes following rename condition new_name = old_name.

library(dplyr)

# Rename the column name of the dataframe

Orange_df = Orange %>% rename(Life_of_tree = age)
Orange_df

Rename the column name “age” with “Life_of_tree”

dplyr rename column name in R 13

 

Rename like using rename_at() function dplyr R:

Rename a specific column that starts with certain character. In our Example we will rename the Column that starts with “T”.  This is accomplished by using rename_at() function along with str_replace() Function as shown below.


library(dplyr)
library(stringr)

# Rename the column name of the dataframe
Orange_df = Orange %>% rename_at(vars(starts_with("T")), 
                                         funs(str_replace(., "Tree", "Tree_Type")))

Rename the column name “Tree” with “Tree_Type”

dplyr rename column name in R 14

 

 

Rename a column with condition using rename_if() function in dplyr R:

Renaming a specific column which satisfies certain condition can be done using rename_if() function . In our Example we will rename the Column only if it is numeric.  This is accomplished by using rename_if() function along with is.numeric() and  str_replace() Function as shown below.


library(dplyr)
library(stringr)

# Rename the column name of the dataframe
Orange_df = Orange %>% rename_if(is.numeric, funs(str_replace(., "age", "tree_life")))
Orange_df

Rename the column name “age” with “tree_life”

dplyr rename column name in R 15

 

 

Rename all the occurrence of a column using rename_all() function

Renaming all the occurrences of a column which satisfies the specific pattern can be done using rename_all() function followed by pattern and followed by replacement character . In our Example we will rename the Column which has “e” as “E” for all the occurrences.  This is accomplished by using rename_all() function along with str_replace_all() Function as shown below.


library(dplyr)
library(stringr)

# Rename the column name of the dataframe
Orange_df = Orange %>% rename_all(funs(str_replace_all(., "e", "E")))
Orange_df

Rename all the column name with pattern “e”  as “E”.

 

dplyr rename column name in R 18

Hope you have enjoyed Dplyr version of renaming. As a Bonus lets look at how to rename the column using Base R package.

Rename Column in R using Base functions:

To rename the column in R we can also use base functions in R instead of dplyr we can accomplish different renaming like renaming all the columns in R and rename the specific column in R. In this tutorial we will be looking on how to

  • Rename all the columns in R
  • Rename only specific column

Let’s first create the dataframe.

df1=data.frame(State=c('Arizona','Georgia', 'Newyork','Indiana','seattle','washington','Texas'),
               code=c('AZ','GA','NY','IN','ST','WT','TX'),
               Score=c(62,47,55,74,31,77,85))
df1

So the dataframe will be

rename column in R 1

 

Rename all the column in R: using base Functions

# rename all the column in R

colnames(df1) = c("State_Name", "State_code","Hindex_Score")
df1

so the resultant dataframe will be

rename column in R 2

 

Rename a specific column in R – Method 1:

Rename the first column as “US_State_Name”

# rename a specific column in R

names(df1)[1] = "US_State_Name"
df1

so the resultant dataframe will be

rename column in R 3

 

Rename a specific column in R – Method 2:

Rename the “State” column as “US_State_Name”

# rename a specific column in R

names(df1)[names(df1) == "State"] <- "US_State_Name" 
df1

so the resultant dataframe will be

rename column in R 4

For Further understanding on how to rename a specific column in R using Dplyr one can refer dplyr documentation


Other related Topics in dplyr :

 

Rename the column name in R using Dplyr                                                                                                          Rename the column name in R using Dplyr

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts