7.4 ifelse()

Using the practice dataset from before, create a new variable called Health with values of sick or healthy:

  • Subject 1 is sick

  • Subject 2 is healthy

  • Subject 3 is healthy

## Method A
practice %>% 
  mutate(Health = ifelse(Subject == 1,
                         "sick",
                         "healthy"))
## # A tibble: 24 x 5
##    Subject Date          DV Inject Health 
##      <dbl> <chr>      <int> <chr>  <chr>  
##  1       1 2019-01-02     9 Pos    sick   
##  2       2 2019-01-02     4 Neg    healthy
##  3       3 2019-01-02     7 Neg    healthy
##  4       1 2019-01-03     8 Neg    sick   
##  5       2 2019-01-03     8 Pos    healthy
##  6       3 2019-01-03     3 Pos    healthy
##  7       1 2019-01-04     3 Pos    sick   
##  8       2 2019-01-04     3 Neg    healthy
##  9       3 2019-01-04     7 Neg    healthy
## 10       1 2019-01-05     6 Neg    sick   
## # ... with 14 more rows
## Method B
practice %>% 
  mutate(Health = ifelse(Subject %in% c(2,3),
                         "healthy",
                         "sick"))
## # A tibble: 24 x 5
##    Subject Date          DV Inject Health 
##      <dbl> <chr>      <int> <chr>  <chr>  
##  1       1 2019-01-02     9 Pos    sick   
##  2       2 2019-01-02     4 Neg    healthy
##  3       3 2019-01-02     7 Neg    healthy
##  4       1 2019-01-03     8 Neg    sick   
##  5       2 2019-01-03     8 Pos    healthy
##  6       3 2019-01-03     3 Pos    healthy
##  7       1 2019-01-04     3 Pos    sick   
##  8       2 2019-01-04     3 Neg    healthy
##  9       3 2019-01-04     7 Neg    healthy
## 10       1 2019-01-05     6 Neg    sick   
## # ... with 14 more rows
## Method C
practice %>% 
  mutate(Health = ifelse(Subject == 1,
                         "sick",
                         ifelse(Subject %in% c(2,3),
                                "healthy",
                                "wtf")))
## # A tibble: 24 x 5
##    Subject Date          DV Inject Health 
##      <dbl> <chr>      <int> <chr>  <chr>  
##  1       1 2019-01-02     9 Pos    sick   
##  2       2 2019-01-02     4 Neg    healthy
##  3       3 2019-01-02     7 Neg    healthy
##  4       1 2019-01-03     8 Neg    sick   
##  5       2 2019-01-03     8 Pos    healthy
##  6       3 2019-01-03     3 Pos    healthy
##  7       1 2019-01-04     3 Pos    sick   
##  8       2 2019-01-04     3 Neg    healthy
##  9       3 2019-01-04     7 Neg    healthy
## 10       1 2019-01-05     6 Neg    sick   
## # ... with 14 more rows
## Method D
practice %>% 
  mutate(Health = ifelse(Subject == 1,
                         "sick",
                         ifelse(Subject == 2,
                                "healthy",
                                "wtf")))
## # A tibble: 24 x 5
##    Subject Date          DV Inject Health 
##      <dbl> <chr>      <int> <chr>  <chr>  
##  1       1 2019-01-02     9 Pos    sick   
##  2       2 2019-01-02     4 Neg    healthy
##  3       3 2019-01-02     7 Neg    wtf    
##  4       1 2019-01-03     8 Neg    sick   
##  5       2 2019-01-03     8 Pos    healthy
##  6       3 2019-01-03     3 Pos    wtf    
##  7       1 2019-01-04     3 Pos    sick   
##  8       2 2019-01-04     3 Neg    healthy
##  9       3 2019-01-04     7 Neg    wtf    
## 10       1 2019-01-05     6 Neg    sick   
## # ... with 14 more rows

7.4.1 Exercises (use practice dataset):

  1. Create a new variable called Group where Subject 1 is in GroupA, Subject 2 is in GroupB, and Subject 3 is in GroupC using:

    • Two ifelse() statements

    • Three ifelse() statements

    • Why can’t we just use one ifelse() statement here?

  2. Create a new variable called Acceptable with yes or no values using ifelse(). For a yes value, the following criteria must be met:

    • When Inject = pos, DV >=

    • When Inject = neg, DV < 5

practice %>% 
  mutate(Acceptable = ifelse(Inject == "Pos", 
                             ifelse(DV >= 5,
                                    "yes",
                                    "no"),
                             ifelse(DV < 5,
                                    "yes",
                                    "no")))
## # A tibble: 24 x 5
##    Subject Date          DV Inject Acceptable
##      <dbl> <chr>      <int> <chr>  <chr>     
##  1       1 2019-01-02     9 Pos    yes       
##  2       2 2019-01-02     4 Neg    yes       
##  3       3 2019-01-02     7 Neg    no        
##  4       1 2019-01-03     8 Neg    no        
##  5       2 2019-01-03     8 Pos    yes       
##  6       3 2019-01-03     3 Pos    no        
##  7       1 2019-01-04     3 Pos    no        
##  8       2 2019-01-04     3 Neg    yes       
##  9       3 2019-01-04     7 Neg    no        
## 10       1 2019-01-05     6 Neg    no        
## # ... with 14 more rows