Home > Software engineering >  Combine if statement with case when in mutate
Combine if statement with case when in mutate

Time:12-06

I have the following table:

df_test <-data.frame(player = c('a', 'b', 'c', 'd', 'e'),
                            position = c('G', 'F', 'F', 'G', 'G'),
                            points = c(9, 15, 19, 22, 32),
                            rebounds = c(5, 7, 7, 12, 11))

Suppose that I want to add new column named rating. The rule of the new column is the following:

  1. Check if the points are more then 10.
  2. Only then, do a case_when to check the other variables: - When rebounds are >=7, we add "average" to the variable rating - When rebounds are >=10, we add "good" to the variable rating - Else, NA

To achieve this, I am using the following code:

df_test%>% mutate(new=if(points>=20){
    case_when(rebounds>=7~"good",
              rebounds>10~"best")
})

However, this is giving me the following error: the condition has length > 1.

How can we combine if and case_when in the same mutate statement, or is there a possibility to write a case_when inside a case_when?

CodePudding user response:

You can add more than one condition inside case_when

library(dplyr)

df_test <-data.frame(player = c('a', 'b', 'c', 'd', 'e'),
                     position = c('G', 'F', 'F', 'G', 'G'),
                     points = c(9, 15, 19, 22, 32),
                     rebounds = c(5, 7, 7, 12, 11))

df_test %>% 
  mutate(
    rating = case_when(
      points > 10 & rebounds >= 10 ~ "good",
      points > 10 & rebounds >= 7  ~ "average",
      TRUE ~ NA_character_
    )
  )

  player position points rebounds  rating
1      a        G      9        5    <NA>
2      b        F     15        7 average
3      c        F     19        7 average
4      d        G     22       12    good
5      e        G     32       11    good

And yes, you also could add another case_when

df_test %>% 
  mutate(
    rating = case_when(
      points > 10 ~ case_when(
        rebounds >= 10 ~ "good",
        rebounds >= 7  ~ "average"
      ),
      TRUE ~ NA_character_
    )
  )
  • Related