Mutate Function in R using dplyr – mutate, mutate_all and mutate_at – Create new variable

Mutate Function in R (mutate, mutate_all and mutate_at) is used to create new variable or column to  the dataframe in R.  Dplyr package in R is provided with mutate(), mutate_all() and mutate_at() function which creates the new variable to the dataframe.

 

Syntax of mutate function in dplyr:

mutate(data_frame, expression(s) )
or
data_frame %>% mutate(expression(s)

We will be using iris data to depict the example of mutate() function

library(dplyr)
mydata2 <-iris 

# Mutate function for creating new variable to the dataframe in R

mydata3 = mutate(mydata2, sepal_length_width_ratio=Sepal.Length/Sepal.Width)
head(mydata3)

New column named sepal_length_width_ratio is created using mutate function and values are populated by dividing sepal length by sepal width

 

mutate_all() Function in R

mutate_all() function in R creates new columns for all the available columns here in our example. mutate_all() function creates 4 new column and get the percentage distribution of sepal length and width, petal length and width.

library(dplyr)
mydata2 <-iris 

# Mutate_all function for creating new variable to the dataframe in R

mydata3 = mutate_all(mydata2[,-5], funs("percent"= ./100))
head(mydata3)

 

mutate_at() Function in R

mutate_at() function in R creates new columns for the specified columns here in our example. mutate_at() Function get the min_rank() of sepal length and sepal width .

library(dplyr)
mydata2 <-iris 

# mutate_at() function for creating new variable to the dataframe in R

mydata4 = mutate_at(mydata2, vars(Sepal.Length,Sepal.Width), funs(Rank=min_rank(desc(.))))
head(mydata4)

 

Create new variable in R using Mutate Function in dplyr                                                                                                          Create new variable in R using Mutate Function in dplyr

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts