Open In App

Rename the column name in R using Dplyr

Last Updated : 21 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to rename the column name using dplyr package in the R programming language.

Dataset in use:

Method 1: Using rename()

This method is used to rename the columns in the dataframe

Syntax:

rename(dataframe,new_columnname=old_column,………….,name,new_columnname=old_columnname)

Where dataframe is the input dataframe, new_columnname is the newname of the column and old_columnname is the old name of the column.

Example: R program to rename single column

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# rename the name column with first_name
data1=rename(data1,first_name=name)
print(data1)
print("====================")
  
# rename the address column with city
rename(data1,city=address)


Output:

Example: R program to rename multiple columns

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns id,name
# and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,first_name=name,roll_no=id,street=address)


Output:

Method 2: Using rename_with()

rename_with() is used to change the case of the column. 

  • uppercase: To convert to uppercase, the name of the dataframe along with the toupper is passed to the function which tells the function to convert the case to upper.

Syntax:

rename_with(dataframe,toupper)

Where, dataframe is the input dataframe and toupper is a keyword that converts all columns to upper

  • lowercase: To convert to lowercase, the name of the dataframe along with the tolower is passed to the function which tells the function to convert the case to lower.

Syntax:

rename_with(dataframe,tolower)

where dataframe is the input dataframe and tolower is a keyword that converts all columns to lower.

Example: R program to convert columns to upper and lower

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns id,name 
# and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# convert all columns to upper
rename_with(data1,toupper)
print("==============")
  
# convert all columns to lower
rename_with(data1,tolower)


Output:

Rename multiple column at once using rename() function

We can rename multiple columns at once using a vector that is by passing columns to vector.

Syntax:

rename(dataframe,c(newcolumn1=oldcolumn1,newcolumn2=oldcolumn2…,,newcolumn n=oldcolumn n))

Example: R program to rename multiple columns at once

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns id,name 
# and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby',
                        'gnanesh','rohith','pinkey',
                        'dhanush','sravan','gnanesh',
                        'ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada','guntur',
                           'hyd','tenali','hyd'))
  
# rename multiple columns
# name with first_name
# id with roll_no
# address with street
rename(data1,c(first_name=name,roll_no=id,street=address))


Output:



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

Similar Reads