Outline for R/data-science workshop

Modified

February 19, 2024

Why R / Tidyverse / Quarto

We’re going to learn about one of the best tool-based ecosystems for orchestrating and sharing our reproducible computational thinking. A major tennant of reproducibility is to do everything with code. This include generating our reports from code (Quarto) as well as importing data, wrangling, modeling, and visualizing data (Tidyverse.)

R and the Tidyverse are a powerful language for data analysis1. In addition to a wide range of built in functions there are thousands of library packages2 we can use in custom programming to extend R into specialized areas of computation.

Based on a weekly schedule, we will learn tidyverse tools and techniques to transform our data into the tidy data format, a consistent format for data organization, manipulation, and analysis.

This workshop series is sponsored by The Center for Data and Visualization Sciences, part of the Duke University Libraries. Videos and links to code, data, and slides for this and many other R workshops can be found at Rfun. These and all other CDVS all workshop resources are available at the CDVS online learning page.

Getting started

  • Reproducibility: data-first tools help leverage reproducible workflows throughout computational thinking

    • Do everything with code

    • Organize our work into RStudio Projects

    • blank-slate instead of .Rdata

    • Restart instead of rm(list = ls())

    • Version Control: git and GitHub

    • {renv} for archiving/documenting package environments

  • Tool suite

    • RStudio

    • Quarto Notebooks (Merge literate coding with report rendering)

    • {dplyr} & {tidyr} - a grammar for data wrangling

    • {ggplot2} - a grammar of graphics

    • Other packages including, for example: {readr}, {forcats}, {stringr}, {purrr}, {tidymodels}

  • Console

    • basic computation

    • vectors and vectorized math

    • environment variables and data variables

    • assignment operators: <- and = (not the same as equivalency ==)

    • pipes = |> and %>%

  • RStudio Projects

    • use projects instead of setwd() ; relative paths are vital to reproducibility
    • Documents: Quarto document
  • Import CSV data

    • read_csv() and the data import wizard found in Files tab and Environment tab

    • Other data formats

      • {readxl}, {haven}
    • SQL

    • Exercise

      • import a CSV file

      • import a Stata categorical file using {haven}

  • {dplyr}

    • select(), filter(), arrange(), mutate(), group_by(), summarize(), count()

    • Exercise

    • Semantic meaning in data 

    • Codebooks in a literate coding context


Visualization plus EDA

  • {ggplot2} viz

    • Exercise
  • basic interactivity - ggplotly()

    • Exercise
  • {broom} and handling modeling outputs (e.g. linear regression)

    • Advertise {tidymodels}

    • Exercise

  • {skimr} and EDA 

    • skim()

    • Reprise group_by() and summarize() 

  • {tidyr} pivot_longer() and pivot_wider()

    • Tall data - what is it?

    • Tall data for iteration: computation 

    • Tall data for visualization , facet_wrap()

    • Tall data for modeling ; {purrr} a first look.

  • join with dplyr::left_join() or other {dplyr} join functions

  • Wrap-up and Summarize the day

Iteration and custom functions

  • Questions from yesterday

  • R is a functional programming language

    • What is functional programing?

      • Customizing functions and iterating without FOR loops
    • FOR loops?

    • Tidyverse preference for data frames

  • Vectorized math (review)

    • Vectorized iteration ; environment variables and data variables
  • Case study - import multiple data files + data cleaning + analysis + visualization

    • read_csv() again
  • Introduce {stringr} and regular expressions

  • Some nifty data wrangling functions

  • Composing custom functions

  • Nesting data for iteration

Review, questions, case study, reports

  • Questions / Review

  • Setup case study

  • Using R to orchestrate data workflows

  • Reproducible report rendering (slides, PDF, dashboards, etc.)

  • Summarize

  • Finding help

Resources

Recommendations

  • Blank slate

  • Use the Projects in the RStudio IDE

  • Use version control (git and GitHub) with {usethis}

  • Use {renv} for package reproducible environments

  • Use {fs} for working with file systems

Footnotes

  1. R is a data-first programming language↩︎

  2. Extend R with domain-specific packages. See: MetaCran, CRAN, github, bioconductor↩︎

Reuse