12.6 Advanced exercises

ds4psy: Exercises on Iteration (12)

Here are some more advanced exercises on functional programming and applying functions to data structures (see Section 12.3):

12.6.1 Exercise A1

Punitive iterations revisited

This exercise asks to run and reflect upon some figures used in this chapter:

  1. Run and explain the code shown in the loopy Bart memes of Figures 12.3 and 12.4.

  2. Run and explain the code shown in the functional programming memes of Figures 12.5 and 12.6.

Note: The post Create Bart Simpson blackboard memes with R at the Learning Machines blog (by Holger K. von Jouanne-Diedrich) explains how to create your own memes.

12.6.2 Exercise A2

Star Wars creatures revisited

This exercise re-uses the starwars data of the dplyr package (Wickham, François, et al., 2023):

sws <- dplyr::starwars %>%
  select(name:mass, species)

In Section 3.2.4, we learned how to use the mutate() function to compute someone’s height in feet (from a given height in centimeters) or their BMI (from given values of height and mass):

# Conversion factor (cm to feet):
factor_cm_2_feet <- 3.28084/100

# Using a mutate() pipe:
sws %>%
  mutate(height_feet = factor_cm_2_feet * height,
         BMI = mass / ((height / 100)  ^ 2),  # compute body mass index (kg/m^2)
         BMI_low  = BMI < 18.5,               # classify low BMI values
         BMI_high = BMI > 30,                 # classify high BMI values
         BMI_norm = !BMI_low & !BMI_high      # classify normal BMI values 
         )
#> # A tibble: 87 × 9
#>    name               height  mass species heigh…¹   BMI BMI_low BMI_h…² BMI_n…³
#>    <chr>               <int> <dbl> <chr>     <dbl> <dbl> <lgl>   <lgl>   <lgl>  
#>  1 Luke Skywalker        172    77 Human      5.64  26.0 FALSE   FALSE   TRUE   
#>  2 C-3PO                 167    75 Droid      5.48  26.9 FALSE   FALSE   TRUE   
#>  3 R2-D2                  96    32 Droid      3.15  34.7 FALSE   TRUE    FALSE  
#>  4 Darth Vader           202   136 Human      6.63  33.3 FALSE   TRUE    FALSE  
#>  5 Leia Organa           150    49 Human      4.92  21.8 FALSE   FALSE   TRUE   
#>  6 Owen Lars             178   120 Human      5.84  37.9 FALSE   TRUE    FALSE  
#>  7 Beru Whitesun lars    165    75 Human      5.41  27.5 FALSE   FALSE   TRUE   
#>  8 R5-D4                  97    32 Droid      3.18  34.0 FALSE   TRUE    FALSE  
#>  9 Biggs Darklighter     183    84 Human      6.00  25.1 FALSE   FALSE   TRUE   
#> 10 Obi-Wan Kenobi        182    77 Human      5.97  23.2 FALSE   FALSE   TRUE   
#> # … with 77 more rows, and abbreviated variable names ¹​height_feet, ²​BMI_high,
#> #   ³​BMI_norm

Using mutate() on the variables of a data table essentially allows computing variables on the fly. However, we often encounter situations in which the functions for computing variables have been defined elsewhere and only need to be applied to the variables in a table. The following steps simuluate this situation:

  1. Create dedicated functions for computing:

    • someone’s height in feet (from height in cm);
    • someone’s body mass index (BMI, from their height and mass); and
    • categorizing their BMI type (as in the mutate() command above).
  1. Apply these functions to all individuals in sws by using appropriate variants of the apply() functions of base R.
  1. Apply these functions to all individuals in sws by using appropriate map() functions from the purrr package (Henry & Wickham, 2023).

This concludes our more advanced exercises on functional programming and applying functions to data structures.

References

Henry, L., & Wickham, H. (2023). purrr: Functional programming tools. Retrieved from https://CRAN.R-project.org/package=purrr
Wickham, H., François, R., Henry, L., Müller, K., & Vaughan, D. (2023). dplyr: A grammar of data manipulation. Retrieved from https://CRAN.R-project.org/package=dplyr