Create, modify, and delete columns using dplyr package in R - GeeksforGeeks

24 Oct, 2021
In this article, we will discuss mutate function present in dplyr package in R Programming Language to create, modify, and delete columns of a dataframe.
Columns can be inserted either by appending a new column or using existing columns to evaluate a new column. By default, columns are added to the far right. Although columns can be added to any desired position using .before and .after arguments
Syntax:
mutate(dataframe , columns)
Parameters:
dataframe is the input dataframe
columns are the new columns that are added to the dataframe
.before(by default = NULL)
.after(by default = NULL)
Example:
R
library(dplyr)  # create a data framed <- data.frame(FirstName=c("Suresh", "Ramesh", "Tanya", "Sujata"),                 Salary=c(50000, 60000, 70000, 80000),                 Expenses=c(20000, 15000, 30000, 25000))  print(d)  # adding new columnsd <- mutate(d, Age=c(25, 28, 22, 27), Savings=Salary - Expenses)  print(d)  # adding a new column before FirstNamed <- mutate(d, Title=c("Mr", "Mr", "Ms", "Ms"), .before=FirstName)print(d)  # adding a new column after FirstNamed <- mutate(d, LastName=c("Singh", "Pande", "Sinha", "Roy"),             .after=FirstName)  print(d)
Output:
FirstName  Salary  Expenses

Suresh       50000   20000
Ramesh     60000   15000
Tanya      70000   30000
Sujata     80000   25000

FirstName  Salary  Expenses Age Savings

Suresh       50000   20000    25  30000
Ramesh     60000   15000    28  45000
Tanya      70000   30000    22  40000
Sujata     80000   25000    27  55000

Title  FirstName  Salary  Expenses Age Savings

Mr     Suresh      50000   20000    25  30000
Mr       Ramesh     60000   15000    28  45000
Ms       Tanya      70000   30000    22  40000
Ms       Sujata     80000   25000    27  55000

Title  FirstName  LastName  Salary  Expenses Age Savings

Mr     Suresh      Singh     50000   20000    25  30000
Mr       Ramesh     Pande     60000   15000    28  45000
Ms       Tanya      Sinha     70000   30000    22  40000
Ms       Sujata     Roy       80000   25000    27  55000  
Columns can be deleted from the existing data frame by setting the value of the desired column to NULL.
Syntax:
mutate(dataframe,columns = NULL)
Parameter:
It takes only one parameter that is column name to be deleted
Example:
R
library(dplyr)  # Create a data framed <- data.frame( FirstName = c("Suresh","Ramesh","Tanya","Sujata"),                 Salary = c(50000,60000,70000,80000),                 Expenses = c(20000,15000,30000,25000))  print(d)  # Delete Expenses columnd <- mutate(d,Expenses = NULL)  print(d)
Output:
FirstName  Salary Expenses

Suresh     50000  20000
Ramesh     60000  15000
Tanya      70000  30000
Sujata     80000  25000

FirstName  Salary 

Suresh     50000  
Ramesh     60000  
Tanya      70000
Sujata     80000 
 
Existing columns can be modified by assigning new values to desired columns.
Syntax:
 mutate(dataframe,column_name=new_values)
Parameters: It will take two parameters
dataframe is the input dataframe
column_name is the name of the column to modify the values
Example:
R
library(dplyr)  # Create a data framed < - data.frame(FirstName=c("Suresh", "Ramesh", "Tanya", "Sujata"),                 Salary=c(50000, 60000, 70000, 80000),                 Expenses=c(20000, 15000, 30000, 25000))  print(d)  # Modify FirstName, Salary  columnd < - mutate(d, FirstName=c("Mahesh", "Jignesh", "Ria", "Tanya"),             Salary=c(60000, 30000, 50000, 75000))  print(d)
FirstName  Salary  Expenses

Suresh       50000   20000
Ramesh     60000   15000
Tanya      70000   30000
Sujata     80000   25000

FirstName  Salary  Expenses 

Mahesh       60000   20000   
Jignesh    30000   15000   
Ria        50000   30000   
Tanya      75000   25000
Picked
R Dplyr
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement