Welcome to Tidyverse

ANU BDSI
workshop
Data Wrangling with R Part 1

Emi Tanaka

Biological Data Science Institute

8th April 2024

Current learning objectives

  • -Recognize the characteristics of tidy data
  • Differentiate between the Base and Tidyverse paradigms
  • Acquire the skills to add/modify columns, subset data by rows and columns, rename column names, and perform group operations using dplyr
  • -Pivot data into longer or wider format using tidyr
  • -Join datasets using dplyr

Base R

  • R has 7 packages, collectively referred to as the “base R”, that are loaded automatically when you launch it.
  • The functions in the base packages are generally well-tested and trustworthy.

Tidyverse

  • Tidyverse refers to a collection of R-packages that share a common (opinionated) design philosophy, grammar and data structure.
  • This trains your mental model to do data science tasks in a manner which may make it easier, faster, and/or fun for you to do these tasks.
  • library(tidyverse) is a shorthand for loading the 9 core tidyverse packages.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Data wrangling with Base R

data.frame (Base R)

  • In R, data.frame is a special class of list.
  • Every column in the data.frame is a vector of the same length.
  • Each entry in a vector have the same type, e.g. logical, integer, double (or numeric), character or factor.
  • It has an attribute row.names which could be a sequence of integers indexing the row number or some unique identifier.

Subsetting by column (Base R)

  • Remember, data.frame is just a special type of list and inherit methods applied for list.
  • data.frame also can be accessed by array index:

Subsetting by column (Base R)

  • If you subset a single column using [, ], then by default the output is a vector and not a data.frame.

Subsetting by row (Base R)

Adding or modifying a column (Base R)

Adding a row (Base R)

What do you notice with the order of the new entry?

Sorting columns (Base R)

Sorting rows (Base R)

Calculating statistical summaries by group (Base R)

🎯 Calculate the average weight (wt) of a car for each gear type in (gear) mtcars

🎯 Calculate the median weight (wt) of a car for each gear (gear) and engine (vs) type in mtcars

Data wrangling with Tidyverse

A grammar of data manipulation

  • dplyr is a core package in tidyverse
  • The earlier concept of dplyr (first on CRAN in 2014-01-29) was implemented in plyr (first on CRAN in 2008-10-08).
  • The functions in dplyr has been evolving frequently but dplyr v1.0.0 was released on CRAN in 2020-05-29 suggesting that functions in dplyr are maturing and thus the user interface is unlikely to change.

dplyr “verbs”

  • The main functions of dplyr include:
    • arrange
    • select
    • mutate
    • rename
    • group_by
    • summarise
  • Notice that these functions are verbs.

dplyr structure

  • Functions in dplyr generally have the form:
verb(data, args)
  • The first argument data is a data.frame object.
  • What do you think the following will do?

Pipe operator

  • Almost all the tidyverse packages use the pipe operator %>% from the magrittr package.
  • R version 4.1.0 introduced a native pipe operator |> which is similar to %>% but with some differences.
  • x |> f(y) is the same as f(x, y).
  • x |> f(y) |> g(z) is the same as g(f(x, y), z).
  • When you see the pipe operator, read it as “and then”.

Tidyselect

  • Tidyverse packages generally use syntax from the tidyselect package for column selection.

Selection language

tibble (Tidyverse)

  • tibble is a modern reimagining of the data.frame object.

Subsetting by column (Tidyverse)

Subsetting by row (Tidyverse)

Adding or modifying a column (Tidyverse)

Adding a row (Tidyverse)

Sorting columns (Tidyverse)

Sorting rows (Tidyverse)

Calculating statistical summaries by group (Tidyverse)

Applying a function to multiple columns

Summary

  • Data wrangling in Tidyverse just gives you a different flavour of how you can do things in Base R.
  • Tidyverse packages share a common design philosophy, grammar and data structure.
  • This trains your mental model to do data science tasks in a certain manner which may make it easier, faster, and/or fun for you to do these tasks.

dplyr cheatsheet

Exercise time

30:00