7.4 ifelse()
Using the practice dataset from before, create a new variable called Health with values of sick or healthy:
Subject 1 is
sickSubject 2 is
healthySubject 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):
Create a new variable called
Groupwhere Subject 1 is inGroupA, Subject 2 is inGroupB, and Subject 3 is inGroupCusing:Two
ifelse()statementsThree
ifelse()statementsWhy can’t we just use one
ifelse()statement here?
Create a new variable called
Acceptablewithyesornovalues usingifelse(). For ayesvalue, 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