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:



Similar Reads

How to Remove a Column by name and index using Dplyr Package in R
In this article, we are going to remove columns by name and index in the R programming language using dplyr package. Dataset in use: Remove a column by using column name We can remove a column with select() method by its column name. Syntax: select(dataframe,-column_name) Where, dataframe is the input dataframe and column_name is the name of the co
2 min read
Rename Columns of a Data Frame in R Programming - rename() Function
In this article, we will discuss how to Rename the Columns of a Data Frame in R Programming Language using the rename() Function. rename function in Rrename() function in R Programming Language is used to rename the column names of a data frame, based on the older names. Syntax: rename(x, names) Parameters: x: Data frame names: Old name and new nam
2 min read
Reorder the column of dataframe in R using Dplyr
In this article, we will discuss how to rearrange or reorder the column of the dataframe using dplyr package in R Programming Language. Creating Dataframe for demonstration: C/C++ Code # load the package library(dplyr) # create the dataframe with three columns # id , department and salary with 8 rows data = data.frame(id = c(7058, 7059, 7060, 7089,
4 min read
How to Remove a Column using Dplyr package in R
In this article, we are going to remove a column(s) in the R programming language using dplyr library. Dataset in use: Remove column using column nameHere we will use select() method to select and remove column by its name. Syntax: select(dataframe,-column_name) Here, dataframe is the input dataframe and column_name is the column in the dataframe t
3 min read
Filter multiple values on a string column in R using Dplyr
In this article we will learn how to filter multiple values on a string column in R programming language using dplyr package. Method 1: Using filter() method filter() function is used to choose cases and filtering out the values based on the filtering conditions. Syntax: filter(df, condition) Parameters: df: Dataframe object condition: filtering ba
3 min read
Mutating column in dplyr using rowSums
In this article, we are going to discuss how to mutate columns in dataframes using the dplyr package in R Programming Language. Installation The package can be downloaded and installed in the R working space using the following command : Install Command - install.packages("dplyr") Load Command - library("dplyr") Functions Usedmutate(): The mutate()
3 min read
Extract specific column from a DataFrame using column name in R
In this article, we are going to see how to extract a specific column from a dataframe using the column name in R Programming Language. In the data.frame() we have to pass dataframe_name followed by $ symbol followed by column name. The reason to pass dataframe_name$ column name to data.frame() is, after extracting the data from column we have to s
5 min read
Group by one or more variables using Dplyr in R
The group_by() method is used to divide and segregate date based on groups contained within the specific columns. The required column to group by is specified as an argument of this function. It may contain multiple column names. Syntax: group_by(col1, col2, ...) Example 1: Group by one variable C/C++ Code # installing required libraries library(
2 min read
Single-Table Analysis with dplyr using R Language
The dplyr package is used to perform simulations in the data by performing manipulations and transformations. It can be installed into the working space using the following command : install.packages("dplyr") Let's create the main dataframe: C/C++ Code #installing the required libraries library(dplyr) #creating a data frame data_frame = data.frame(
5 min read
How to Create Frequency Table by Group using Dplyr in R
In this article, we will be looking at the approach to creating a frequency table group with its working examples in the R programming language. Create Frequency Table by Group using dplyr package: In this approach to create the frequency table by group, the user first needs to import and install the dplyr package in the working console, and then t
2 min read
Article Tags :