11.2 Essentials

Link to recommended readings:

Summarize in three subsections:

  • Basic conditionals: if(){} and if(){} else {}
  • Advanced conditionals: Using switch() and vectorized ifelse()
  • 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():

v <- 1:5
v
#> [1] 1 2 3 4 5

v > 2
#> [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: -----  
name <- c("Adam", "Bertha", "Cecily", "Dora", "Eve", "Nero", "Zeno")
sex  <- c("male", "female", "female", "female", "female", "male", "male")
age  <- c(21, 23, 22, 19, 21, 18, 24)
height <- c(165, 170, 168, 172, NA, 185, 182)

# Combine 4 vectors (of equal length) into a data frame: 
dt <- tibble::tibble(name, sex, age, height)

knitr::kable(dt, caption = "Basic information on seven people.")
Table 11.1: Basic information on seven people.
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
dt$gender <- NA  # initialize variable
dt$gender
#> [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}

dt$gender
#> [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: 
dt$gender[dt$sex == "male"]   <- 1
dt$gender[dt$sex == "female"] <- 2
dt$gender
#> [1] 1 2 2 2 2 1 1

Solution 2: Using ifelse()

# Using ifelse():
dt$gender <- NA  # re-initialize variable
# dt$gender

dt$gender <- ifelse(dt$sex == "male", 1, 2)
dt$gender
#> [1] 1 2 2 2 2 1 1

References

Neth, H. (2021a). 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