Skip to content

Data Science Tutorials

For Data Science Learners

  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • Error-list-object-cannot-be-coerced-to-type-double
    Error-list-object-cannot-be-coerced-to-type-double R
  • How to Recode Values in R
    How to Recode Values in R R
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • bootstrapping-in-r
    Bootstrapping in R R
How to do Conditional Mutate in R

How to do Conditional Mutate in R?

Posted on July 13 By Admin No Comments on How to do Conditional Mutate in R?

How to do Conditional Mutate in R, It’s common to wish to add a new variable based on a condition to an existing data frame. The mutate() and case when() functions from the dplyr package make this task fortunately simple.

Cumulative Sum calculation in R – Data Science Tutorials

With the following data frame, this lesson provides numerous examples of how to apply these functions.

How to do Conditional Mutate in R

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4', 'P5'),
position = c('A', 'B', 'A', 'B', 'B'),
points = c(102, 215, 319, 125, 112),
rebounds = c(22, 12, 19, 23, 36))

Let’s view the data frame

df
   player position points rebounds
1     P1        A    102       22
2     P2        B    215       12
3     P3        A    319       19
4     P4        B    125       23
5     P5        B    112       36

Example 1: Based on one existing variable, create a new variable

A new variable called “score” can be created using the following code depending on the value in the “points” column.

Top Data Science Skills to Get You Hired »

library(dplyr)

Let’s define new variable ‘score’ using mutate() and case_when()

df %>%
  mutate(score = case_when(points < 105 ~ 'LOW',
  points < 212 ~ 'MED',
  points < 450 ~ 'HIGH'))
  player position points rebounds score
1     P1        A    102       22   LOW
2     P2        B    215       12  HIGH
3     P3        A    319       19  HIGH
4     P4        B    125       23   MED
5     P5        B    112       36   MED

Example 2: Based on a number of existing variables, create a new variable

The following code demonstrates how to make a new variable called “type” based on the player and position values in the player column.

Tips for Rearranging Columns in R – Data Science Tutorials

library(dplyr)

Now we can define the  new variable ‘Type’ using mutate() and case_when()

df %>%
  mutate(Type = case_when(player == 'P1' | player == 'P2' ~ 'starter',
  player == 'P3' | player == 'P4' ~ 'backup',
  position == 'B' ~ 'reserve'))
   player position points rebounds    Type
1     P1        A    102       22 starter
2     P2        B    215       12 starter
3     P3        A    319       19  backup
4     P4        B    125       23  backup
5     P5        B    112       36 reserve

In order to generate a new variable called “value” depending on the value in the points and rebounds columns, use the following code.

Best online course for R programming – Data Science Tutorials

library(dplyr)

Let’s define the new variable ‘value’ using mutate() and case_when()

df %>%
  mutate(value = case_when(points <= 102 & rebounds <=45 ~ 2,
  points <=215 & rebounds > 55 ~ 4,
  points < 225 & rebounds < 28 ~ 6,
  points < 325 & rebounds > 29 ~ 7,
  points >=25 ~ 9))
player position points rebounds value
1     P1        A    102       22     2
2     P2        B    215       12     6
3     P3        A    319       19     9
4     P4        B    125       23     6
5     P5        B    112       36     7

Hope now you are clear with the concept.

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Random Forest Machine Learning Introduction
Next Post: Subset rows based on their integer locations-slice in R

Related Posts

  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • Checking Missing Values in R
    Checking Missing Values in R R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • Compare numeric vectors in R R
  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R
  • Understanding Machine Learning and Data Science R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • R-Change Number of Bins in Histogram R
  • OLS Regression in R
    OLS Regression in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Predict potential customer in R
    Predict potential customers in R R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Number to Percentage in R
    Number to Percentage in R R
  • Type II Error in R
    Type II Error in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme