Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • How do confidence intervals work
    How do confidence intervals work? R
  • Difference between R and Python
    Difference between R and Python R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • How to use image function in R
    How to use the image function in R R
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
How to do Conditional Mutate in R

How to do Conditional Mutate in R?

Posted on July 13 By Jim No Comments on How to do Conditional Mutate in R?
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • best books about data analytics
    Best Books to learn Tensorflow Course
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R
  • How to Use Italic Font in R
    How to Use Italic Font in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Count Observations by Group in R
    Count Observations by Group in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning
  • Box Cox transformation in R
    Box Cox transformation in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme