Tidying and reshaping – exercises

Day 3, B

Practice manipulating data by tidying and reshaping
Author

Michael C Sachs

Learning objectives

In this lesson you will

  1. Practice tidying statistical results
  2. See and understand how to reshape data from wide to long and long to wide

Tidying our mean sd function

Load the broom package and look at the source code for tidy.lm

broom:::tidy.lm
function (x, conf.int = FALSE, conf.level = 0.95, exponentiate = FALSE, 
    ...) 
{
    warn_on_subclass(x, "tidy")
    ret <- as_tibble(summary(x)$coefficients, rownames = "term")
    colnames(ret) <- c("term", "estimate", "std.error", "statistic", 
        "p.value")
    coefs <- stats::coef(x)
    if (length(coefs) != nrow(ret)) {
        coefs <- tibble::enframe(coefs, name = "term", value = "estimate")
        ret <- left_join(coefs, ret, by = c("term", "estimate"))
    }
    if (conf.int) {
        ci <- broom_confint_terms(x, level = conf.level)
        ret <- dplyr::left_join(ret, ci, by = "term")
    }
    if (exponentiate) {
        ret <- exponentiate(ret)
    }
    ret
}
<bytecode: 0x5e9b84747588>
<environment: namespace:broom>
  1. Write a tidy method for our mean_sd function and try it out on the penguins dataset.
  2. Apply the mean_sd function to the penguins body mass in grams by species and sex. Organize the results into a table suitable for publication, where it is easy to compare the two sexes.

Tidying the national patient register dataset

Load the LPR data example from "https://sachsmc.github.io/r-programming/data/lpr-ex.rds"

library(here)
here() starts at /home/sachsmc/Teaching/Courses/r-programming
lpr <- readRDS(here("data", "lpr-ex.rds"))

Use the tidy principles to do the following:

  1. Reshape the data into wide, where the columns are the primary diagnosis (hdia) at each visit number
  2. Reshape the data into longer format, where all of the diagnoses are stored in a single variable, with another variable indicating the primary diagnosis.
  3. Create a new variable for each participant which equals TRUE if they had any diagnosis of either D150, D152, or D159 before the date 1 January 2010.

Merging and manipulation

The objective of this project is to describe the distribution of the number of days between hospitalizations and drug dispensations by age and sex. Your challenge is to do the following:

  1. Import and merge the drug register data with the hospitalization register.
  2. Create a new variable that counts the number of drug dispensations in the 3 months following a hospitalization.
  3. Summarize the variable by age and sex. Try making a graphical summary .
Hints
  • The drug register data are stored in separate files by year. You will need to iterate over these files somehow, maybe using a loop or one of the apply functions.
  • The file names can be created programmatically with paste0("med-", 2005:2010, "-ex.rds")
  • Once they are all read in as objects, you will want to append them by row, using e.g., rbind
  • Join the hospitalization table to the drug table, by patient id. How to deal with the dates? We want only the most recent prescription since the last hospitalization. This is a rolling join