Trusted answers to developer questions

What is the mutate() function in R?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

Syntax

mutate(x, expr)

Parameters

  • x: Contains the data frame.
  • expr: The operation to be performed on variables.

Example 1

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 frame
df <- 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)
Expected output

Example 2

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 frame
df <- data.frame(
COL1 = c(5, 8, 9, 6),
COL2= c(55, 6 , NA, 45)
)
# Calculate the product (dotProduct) which is COL1*COL2
mutate(df, dotProduct = COL1 * COL2)
Expected output

RELATED TAGS

r
Did you find this helpful?