We can use the mutate()
function in R programming to add new variables in the specified data frame. These new variables are added by performing the operations on present variables.
Before using the mutate()
function, you need to install the dplyr
library. We can use the mutate()
method to manipulate big datasets. mutate()
is a rather simple method to use to manipulate datasets.
install.package("dplyr")
This can be used to install the
dplyr
library.
mutate(x, expr)
x
: Contains the data frame.expr
: The operation to be performed on variables.In the example below, we have a data frame df
that contains four students’ marks in Maths
and English
. Our major goal is to use the mutate()
method and make a new data frame that contains the totalMarks
column.
library(dplyr) #load the library# Creating data framedf <- data.frame(studentname = c("Student1", "Student2", "Student3", "Student4"),Math = c(75, 58, 93, 66),Eng= c(44, 89, 89, NA))# Calculate the total marks (totalMarks)# sum of marks in Maths (Math) & English (Eng)mutate(df, totalMarks = Math + Eng)
In the example below, we are simply multiplying two columns, COL1, COL2
, into a new column named dotProduct
. We use the mutate()
method to merge these three columns.
library(dplyr) #load the library# Creating data framedf <- data.frame(COL1 = c(5, 8, 9, 6),COL2= c(55, 6 , NA, 45))# Calculate the product (dotProduct) which is COL1*COL2mutate(df, dotProduct = COL1 * COL2)