knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Even though we were trying to get everything right the first time, small errors can (and do!) slip through the cracks. Here we keep an updated list of errors and mistakes that have been gracefully pointed out to us by attentive readers.

Problems in the text

Problems with code

library(dplyr)
library(astatur)
workout <- mutate(workout, healthage = health * age)

The mutate function may give an error (with recent versions of R and the dplyr package) that it cannot compute the product of health and age, since health is of type haven_labelled. The problem can be solved by applying the function zap_formats():

workout <- haven::zap_formats(workout)
country <- factor(c("Dutch", "Wales", "Scotland", "Holland",
                    "Netherlands", "United Kingdom", "England",
                    "Northern Ireland"))
country <- fct_collapse(country,
                        Netherlands = c("Friesland", "Holland", "Netherlands"),
                        `United Kingdom` = c("Wales", "Scotland",
                                             "United Kingdom", "England",
                                             "Northern Ireland"))                    

Running this code will collapse the factor to categories "Netherlands", "United Kingdom" and "Dutch". Our intention was that category "Dutch" from the factor specification should be subsumed in the "Netherlands" category. Therefore, "Dutch" should be relabeled as "Friesland" or "Dutch" should be included in the list in the second statement using fct_collapse().

depression.wide %>% 
  pivot_longer(cols=starts_with("week") | starts_with("BDI"),
               names_to = c(".value", "session"),
               names_pattern="(BDI|week)([0-9+])",
               values_drop_na=T) %>%
  head()

There is a mistake in the line names_pattern="(BDI|week)([0-9+])". The regular expression [0-9+] only captures a single integer and hence the code will not work as intended as values such as "BDI10" or "week12" will be coded as session=1 instead of session=10 and session=12, respectively. The correct regular expression should be "(BDI|week)([0-9]+)" such that the full code-example should read

depression.wide %>% 
  pivot_longer(cols=starts_with("week") | starts_with("BDI"),
               names_to = c(".value", "session"),
               names_pattern="(BDI|week)([0-9]+)",
               values_drop_na=T) %>%
  head()

Acknowledgments

Thanks to Dan Mønster (http://au.dk/en/danm@econ.au.dk) for thoroughly reading and commenting on the book!

Thanks to Benjamin Holen Dybendal (https://www.ntnu.no/ansatte/benjamin.dybendal) for pointing out errors in the book.



ihrke/astatur documentation built on Dec. 13, 2024, 8:42 a.m.