11.2 Essentials
Link to recommended readings:
- Section 11.3: Conditionals of the ds4psy book (Neth, 2022a).
- Section 19.4: Conditional execution of the r4ds book (Wickham & Grolemund, 2017).
Summarize in three subsections:
- Basic conditionals:
if(){}
andif(){} else {}
- Advanced conditionals: Using
switch()
and vectorizedifelse()
- Alternatives: Using indexing rather than conditionals in R.
11.2.1 Basic conditionals
Key commands:
if(){}
,if(){} else {}
11.2.2 Advanced conditionals
Key commands:
switch()
ifelse()
Difference between if-then vs. ifelse()
:
<- 1:5
v
v#> [1] 1 2 3 4 5
> 2
v #> [1] FALSE FALSE TRUE TRUE TRUE
# if then: assumes a test that yields 1 truth value:
if (v > 2) {"big"} else {"small"}
#> [1] "small"
# ifelse:
ifelse(v > 2, "big", "small")
#> [1] "small" "small" "big" "big" "big"
11.2.3 Avoiding conditionals
Lesson to learn: In R, we often use vector indexing/subsetting, rather than conditionals.
# Data:
# Create some data: -----
<- c("Adam", "Bertha", "Cecily", "Dora", "Eve", "Nero", "Zeno")
name <- c("male", "female", "female", "female", "female", "male", "male")
sex <- c(21, 23, 22, 19, 21, 18, 24)
age <- c(165, 170, 168, 172, NA, 185, 182)
height
# Combine 4 vectors (of equal length) into a data frame:
<- tibble::tibble(name, sex, age, height)
dt
::kable(dt, caption = "Basic information on seven people.") knitr
name | sex | age | height |
---|---|---|---|
Adam | male | 21 | 165 |
Bertha | female | 23 | 170 |
Cecily | female | 22 | 168 |
Dora | female | 19 | 172 |
Eve | female | 21 | NA |
Nero | male | 18 | 185 |
Zeno | male | 24 | 182 |
Define a new numeric variable gender
.
Let’s first initialize it to some NA
value:
# Initialize gender variable:
# dt$gender <- rep(NA, length(dt$sex)) # initialize variable
$gender <- NA # initialize variable
dt$gender
dt#> [1] NA NA NA NA NA NA NA
Goal: Value of gender
should be set to 1 when sex
is “male,” and set to 2 when sex
is “female.”
Note that this creates a binary variable, which happens to suffice for the 7 people in dt
, but reality is more complex.
Bad/false approach (often prevalent in imperative/SPSS-based thinking):
# Erroneous conditionals:
if (dt$sex == "male") {dt$gender <- 1}
if (dt$sex == "female") {dt$gender <- 2}
$gender
dt#> [1] 1 1 1 1 1 1 1
Note the warnings:
Only 1st element of test is evaluated. All elements of dt$sex
are set to 1.
Solution 1: Using indexing/subsetting:
# Solution by logical indexing/subsetting:
$gender[dt$sex == "male"] <- 1
dt$gender[dt$sex == "female"] <- 2
dt$gender
dt#> [1] 1 2 2 2 2 1 1
Solution 2: Using ifelse()
# Using ifelse():
$gender <- NA # re-initialize variable
dt# dt$gender
$gender <- ifelse(dt$sex == "male", 1, 2)
dt$gender
dt#> [1] 1 2 2 2 2 1 1
References
Neth, H. (2022a). Data science for psychologists. Social Psychology; Decision Sciences, University of Konstanz. https://bookdown.org/hneth/ds4psy/
Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. O’Reilly Media, Inc. http://r4ds.had.co.nz