Working with Data in the Tidyverse

MY202 · Lab 1

Jonas Weinert

Department of Methodology  |  London School of Economics and Political Science

1 October 2026

Introductions

1Your name
2Your programme and year
3Previous experience with R, statistics, machine learning, and coding with AI tools: none is a fine answer
4One thing you are passionate about

Lab overview

Part Topic Functions Data Time
Introductions and setup 11 min
0 R basics objects, functions, vectors, data frames, lists 6 min
1 Summarising data count, mutate, group_by, summarise agadullina2018: 85 effect sizes, out-group entitativity and prejudice 22 min
2 Tidy data and reshaping pivot_longer, pivot_wider World Bank life expectancy, 1960–2024 25 min
3 Iteration map, bind_rows maldonado2020: 1,268 effect sizes, age and executive functioning 20 min

Each part: a short demonstration, exercises in the notebook, then the solutions.

Setup

library("ggsci")
library("gt")
library("psymetadata")
library("tidyverse")

The notebook mixes text and code chunks (the grey boxes). Run a chunk with the green ▶ button, or Ctrl/Cmd + Shift + Enter. The output appears below it.

The Functions Used table at the top of the notebook is your reference sheet for today.

Missing package install.packages("psymetadata"), then run the chunk again

Conflict messages tidyverse reports functions it masks, such as filter(). These can be ignored.

R basics

Before we start

Objects · functions · vectors · data frames · lists

Objects, functions and packages

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
x <- 5          # store 5 as object x
x * 2           # returns 10

round(3.14159, digits = 2)
                # function: returns 3.14

library("dplyr")  # load a package
?round            # open the help page

Object A named value stored in memory. <- assigns: it creates or overwrites the object. Nothing is kept unless you assign it.

Function and arguments A function takes inputs, called arguments, inside ( ) and returns an output. Arguments can be named: digits = 2.

Package A collection of functions written by others. Install once with install.packages(), load in every session with library().

Vectors, data frames and lists

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
Vector
39.549.356.3
c(39.5, 49.3, 56.3) Several values of one type in a sequence. c() combines values into a vector.
Data frame · tibble
countrylifeexpNiger57.1Honduras69.8Suriname69.6
tibble(country = ..., lifeexp = ...) A table. Each column is a vector, all of the same length. Rows are observations, columns are variables. A tibble is a data frame that prints more neatly.
List
a data framea vectoranother data frame
list(df1, x, df2) A container whose elements can be any kind of object, of any length. Used in Part 3.

Types, missing values and comparisons

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
Type Example Note
numeric 57.1 numbers
character "Niger" text, always in quotes
logical TRUE, FALSE results of comparisons
missing NA value not available

"1970" in quotes is text, not a number. class() tells you the type.

Operator Meaning Example
== equal to year == 1990
!= not equal to design != "Experiment"
> < greater, less than n > 100
%in% is one of year %in% c(1970, 1990)

= versus ==

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

= sets an argument round(3.14159, digits = 2)

== asks a question Is year equal to 1990? TRUE or FALSE for every row. filter() keeps the rows where the answer is TRUE.

Error in `filter()`:
We detected a named input.
This usually means that you've
used `=` instead of `==`.

Summarising data with dplyr

Part 1

count() · mutate() · group_by() · summarise()

Inspecting a data set

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
?agadullina2018          # documentation
class(agadullina2018)    # "data.frame"

agadullina2018_tbl <- as_tibble(agadullina2018)
agadullina2018_tbl       # prints 10 rows + types
glimpse(agadullina2018_tbl)

Data A meta-analysis: 85 effect sizes from 21 studies on out-group entitativity and prejudice. An effect size measures the strength of a relationship on a common scale.

Key variables yi is the effect size (Fisher’s z), vi is its variance

Exam tip: never print a whole data frame. Use the tibble print or glimpse().

The pipe operator |>

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Nested

mutate(count(agadullina2018_tbl, design),
       pct = n / sum(n) * 100)

Read from the inside out

Piped

agadullina2018_tbl |>
  count(design) |>
  mutate(pct = n / sum(n) * 100)

Read top to bottom: each step passes its result to the next

Keyboard shortcut: Ctrl + Shift + M (Windows), Cmd + Shift + M (Mac).

Core dplyr verbs

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

count How often does each value appear?

count(design)

mutate Add or change a column

mutate(pct = ...)

summarise Collapse many rows into one

summarise(yi = mean(yi))

group_by Do the next step once per group

group_by(design)

filter · select Keep some rows · keep some columns

filter(n > 100)

Each verb takes a data frame as its first argument and returns a data frame.

Grouped summaries with group_by()

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
agadullina2018_tbl |>
  summarise(yi = mean(yi))
yi
0.409

One row for the whole data set

agadullina2018_tbl |>
  group_by(design) |>
  summarise(yi = mean(yi))
design yi
Cross-sectional 0.468
Experiment 0.111

One row per group same code, one extra line

Exercises: Part 1

Part 1 · notebook Q1–11

  1. ?agadullina2018: read the documentation
  2. class(): what kind of object is it?
  3. Print it (just this once)
  4. as_tibble() with a new name, then print
  5. count() the frequencies of design
  6. Rewrite step 5 with a pipe |>
  7. Pipe into mutate(): add a percentage column
  8. summarise() the mean of yi
  9. Add group_by(design) before summarise()
  10. What have we found out?

Syntax you need

df_tbl <- as_tibble(df)          # assign with <-

df_tbl |>
  count(design) |>               # n per group
  mutate(pct = n / sum(n) * 100) # new column

df_tbl |>
  group_by(design) |>            # per group...
  summarise(yi = mean(yi))       # ...one row each

Stuck?

  • object not found: did you run the line with <-?
  • could not find function: run the setup chunk
  • Only one row? You forgot group_by()
  • Done early? Try glimpse(), or filter(n > 200) first

Solutions: Part 1 · Q2–4

class(agadullina2018)

agadullina2018_tbl <- as_tibble(agadullina2018)
agadullina2018_tbl

Find out what kind of object the data is, then convert it to a tibble and look at it.

class() asks R what kind of object this is. "data.frame" means it is a table.

[1] "data.frame"

as_tibble() converts the data frame to a tibble, and <- saves it under a new name. Typing the name prints it: 85 rows and 9 columns, with each column’s type under its name.

# A tibble: 85 × 9
  es_id study_id authors     pub_year     n design
  <int>    <int> <chr>       <chr>    <int> <chr> 
1     1        1 Agadullina… 2017       193 Cross…
2     2        1 Agadullina… 2017       193 Cross…
3     3        1 Agadullina… 2017       193 Cross…
4     4        1 Agadullina… 2017       193 Cross…
# ℹ 81 more rows
# ℹ 3 more variables: ent_alpha <dbl>, yi <dbl>,
#   vi <dbl>

Solutions: Part 1 · Q5–7

agadullina2018_tbl |>
  count(design) |>
  mutate(pct = (n / sum(n)) * 100)

Count how many effect sizes come from each study design, then express the counts as percentages.

Start with the data: one row per effect size.

# A tibble: 85 × 9
  es_id study_id authors     pub_year     n design
  <int>    <int> <chr>       <chr>    <int> <chr> 
1     1        1 Agadullina… 2017       193 Cross…
2     2        1 Agadullina… 2017       193 Cross…
3     3        1 Agadullina… 2017       193 Cross…
4     4        1 Agadullina… 2017       193 Cross…
# ℹ 81 more rows
# ℹ 3 more variables: ent_alpha <dbl>, yi <dbl>,
#   vi <dbl>

count(design) counts the rows for each value of design. The result is a new, small table with the counts in a column called n: 71 cross-sectional, 14 experiments.

# A tibble: 2 × 2
  design              n
  <chr>           <int>
1 Cross-sectional    71
2 Experiment         14

mutate() adds a column. pct divides each count by the total and multiplies by 100: 83.5% and 16.5%.

# A tibble: 2 × 3
  design              n   pct
  <chr>           <int> <dbl>
1 Cross-sectional    71  83.5
2 Experiment         14  16.5

Q5, count(agadullina2018_tbl, design), is the same as the first two lines without the pipe.

Solutions: Part 1 · Q8–9

agadullina2018_tbl |>
  group_by(design) |>
  summarise(yi = mean(yi))

Calculate the average effect size separately for each study design.

Start with the data: one row per effect size.

# A tibble: 85 × 9
  es_id study_id authors     pub_year     n design
  <int>    <int> <chr>       <chr>    <int> <chr> 
1     1        1 Agadullina… 2017       193 Cross…
2     2        1 Agadullina… 2017       193 Cross…
3     3        1 Agadullina… 2017       193 Cross…
4     4        1 Agadullina… 2017       193 Cross…
# ℹ 81 more rows
# ℹ 3 more variables: ent_alpha <dbl>, yi <dbl>,
#   vi <dbl>

group_by(design) sorts the rows into groups, one per design. The data look the same: only the “Groups” line in the output shows that something changed.

# A tibble: 85 × 9
# Groups:   design [2]
  es_id study_id authors     pub_year     n design
  <int>    <int> <chr>       <chr>    <int> <chr> 
1     1        1 Agadullina… 2017       193 Cross…
2     2        1 Agadullina… 2017       193 Cross…
3     3        1 Agadullina… 2017       193 Cross…
4     4        1 Agadullina… 2017       193 Cross…
# ℹ 81 more rows
# ℹ 3 more variables: ent_alpha <dbl>, yi <dbl>,
#   vi <dbl>

summarise() collapses each group into one row and computes the mean of yi within it. Cross-sectional studies average 0.468, experiments 0.111.

# A tibble: 2 × 2
  design             yi
  <chr>           <dbl>
1 Cross-sectional 0.468
2 Experiment      0.111

Q8 is the same without line 2: one row, the mean of all 85 effect sizes.

Part 1: results

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

How many effect sizes per design?

Cross-sectional71 · 84%
Experiment14 · 16%

Mean effect size (yi)

Cross-sectional0.468
Experiment0.111

Result 84% of effect sizes come from cross-sectional designs, which report a larger mean effect than experiments (0.468 vs 0.111)

Caveat An unweighted mean gives every effect size equal weight. Meta-analytic estimates weight by precision (vi), covered later in the course.

Tidy data and reshaping

Part 2

pivot_longer() · pivot_wider() · parse_number()

Tidy data

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Illustration: in tidy data, each variable forms a column, each observation forms a row, and each cell is a single measurement

Illustration: Julia Lowndes and Allison Horst, Tidy Data for reproducibility, efficiency, and collaboration, Openscapes blog, 2020. CC BY 4.0.

Messy data is messy in its own way

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Illustration: tidy datasets look alike and happy; messy datasets each have their own problem, such as values in column names or several variables in one column

Illustration: Julia Lowndes and Allison Horst, Tidy Data for reproducibility, efficiency, and collaboration, Openscapes blog, 2020. CC BY 4.0.

Wide and long formats

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Wide 263 rows × 67 columns

country_name 1970 1990 2010 …
Burkina Faso 39.5 49.3 56.3 …
Honduras 54.4 65.2 69.8 …
Niger 35.9 41.7 57.1 …

Year is a variable, but it’s stuck in the headers. You can’t filter(year == 1990).

Long (tidy) 17,358 rows × 3 columns

country_name year lifeexp
Burkina Faso 1970 39.5
Burkina Faso 1990 49.3
Burkina Faso 2010 56.3
Honduras 1970 54.4
… … …

Wide format suits presentation tables; long format suits analysis.

Reshaping with pivot_longer() and pivot_wider()

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
lifeexp |>
  pivot_longer(-country_name,          # 1. which columns to stack? all but country
               names_to  = "year",     # 2. where do the old headers go?
               values_to = "lifeexp") |>  # 3. where do the numbers go?
  mutate(year = parse_number(year))    # "1970" (text) -> 1970 (number)

pivot_longer() Wide → long, for analysis

pivot_wider() Long → wide, for a results table: names_from = year, values_from = lifeexp

Data quality issues in the World Bank file

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Aggregates

About 46 rows are regional or income-group aggregates (“World”, “High income”, “Euro area”).

Exclude them before averaging across countries.

Empty column

The file has a 2025 column with no values, so pivoting creates 263 rows of NA.

Remove with drop_na() or values_drop_na = TRUE.

Exercises: Part 2

Part 2 · notebook Q1–8

  1. read_csv() the file, rename() column 1 to country_name
  2. pull() the countries: spot the non-countries
  3. pivot_longer() everything except country_name
  4. parse_number() the year column
  5. filter() four countries in 1970, 1990, 2010
  6. pivot_wider() back to one column per year
  7. gt() it into a formatted table
  8. What have you found out?

Syntax you need

lifeexp <- read_csv("data/wb-life-exp.csv") |>
  rename(country_name = 1)

lifeexp |>
  pivot_longer(-country_name, names_to = "year",
               values_to = "lifeexp") |>
  mutate(year = parse_number(year)) |>
  filter(country_name %in% c("Niger", "Honduras"),
         year %in% c(1970, 1990, 2010)) |>
  pivot_wider(names_from = year,
              values_from = lifeexp) |>
  gt()

Stuck?

  • does not exist in current working directory: check the path. Is the CSV in a data/ folder?
  • Use %in% with a vector, not ==
  • A country returns nothing? Copy its exact spelling from pull()

Solutions: Part 2 · Q1–2

lifeexp <- read_csv("wb-life-exp.csv") |>
  rename(country_name = 1)

pull(lifeexp, country_name) |>
  head(12)

Load the World Bank file, give the first column a usable name, and check which rows it contains.

read_csv() reads the file into a tibble. The first column is called Country Name, with a space, which is awkward to use in code.

# A tibble: 263 × 67
  `Country Name`       `1960` `1961` `1962` `1963`
  <chr>                 <dbl>  <dbl>  <dbl>  <dbl>
1 Aruba                  64.0   64.2   64.6   64.9
2 Africa Eastern and …   44.2   44.5   44.9   45.2
3 Afghanistan            32.8   33.3   33.8   34.2
4 Africa Western and …   37.8   38.1   38.7   38.9
# ℹ 259 more rows
# ℹ 62 more variables: `1964` <dbl>,
#   `1965` <dbl>, `1966` <dbl>, `1967` <dbl>, …

rename() takes new name = old column; here the old column is given by its position, 1. <- saves the result as lifeexp.

# A tibble: 263 × 67
  country_name  `1960` `1961` `1962` `1963` `1964`
  <chr>          <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Aruba           64.0   64.2   64.6   64.9   65.3
2 Africa Easte…   44.2   44.5   44.9   45.2   45.5
3 Afghanistan     32.8   33.3   33.8   34.2   34.7
4 Africa Weste…   37.8   38.1   38.7   38.9   39.2
# ℹ 259 more rows
# ℹ 61 more variables: `1965` <dbl>,
#   `1966` <dbl>, `1967` <dbl>, `1968` <dbl>, …

pull() extracts one column as a vector, and head(12) keeps the first 12 values. Some are regions, such as “Arab World”: not every row is a country.

 [1] "Aruba"                      
 [2] "Africa Eastern and Southern"
 [3] "Afghanistan"                
 [4] "Africa Western and Central" 
 [5] "Angola"                     
 [6] "Albania"                    
 [7] "Andorra"                    
 [8] "Arab World"                 
 [9] "United Arab Emirates"       
[10] "Argentina"                  
[11] "Armenia"                    
[12] "American Samoa"             

The path assumes the CSV sits next to the notebook. head(12) shortens the list of 263 names for the slide.

Solutions: Part 2 · Q3–7

lifeexp |>
  pivot_longer(-country_name,
               names_to = "year",
               values_to = "lifeexp") |>
  mutate(year = parse_number(year),
         lifeexp = round(lifeexp, 1)) |>
  filter(country_name %in% c("Burkina Faso",
           "Honduras", "Niger", "Suriname"),
         year %in% c(1970, 1990, 2010)) |>
  pivot_wider(id_cols = country_name,
              names_from = year,
              values_from = lifeexp) |>
  gt()

Reshape the table from wide to long, keep four countries and three years, then present them as a table.

The wide table: one row per country, one column per year.

# A tibble: 263 × 67
  country_name  `1960` `1961` `1962` `1963` `1964`
  <chr>          <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Aruba           64.0   64.2   64.6   64.9   65.3
2 Africa Easte…   44.2   44.5   44.9   45.2   45.5
3 Afghanistan     32.8   33.3   33.8   34.2   34.7
4 Africa Weste…   37.8   38.1   38.7   38.9   39.2
# ℹ 259 more rows
# ℹ 61 more variables: `1965` <dbl>,
#   `1966` <dbl>, `1967` <dbl>, `1968` <dbl>, …

pivot_longer() stacks every column except country_name into two columns: year holds the old column names, lifeexp the values. The table now has 17,358 rows, one per country and year.

# A tibble: 17,358 × 3
  country_name year  lifeexp
  <chr>        <chr>   <dbl>
1 Aruba        1960     64.0
2 Aruba        1961     64.2
3 Aruba        1962     64.6
4 Aruba        1963     64.9
# ℹ 17,354 more rows

mutate() changes two columns: parse_number() turns year from text into a number, and round() keeps one decimal of life expectancy.

# A tibble: 17,358 × 3
  country_name  year lifeexp
  <chr>        <dbl>   <dbl>
1 Aruba         1960    64  
2 Aruba         1961    64.2
3 Aruba         1962    64.6
4 Aruba         1963    64.9
# ℹ 17,354 more rows

filter() keeps rows where the country is one of four names and the year is one of three. %in% checks whether each value is in the vector. 12 rows remain.

# A tibble: 12 × 3
  country_name  year lifeexp
  <chr>        <dbl>   <dbl>
1 Burkina Faso  1970    39.5
2 Burkina Faso  1990    49.3
3 Burkina Faso  2010    56.3
4 Honduras      1970    54.4
# ℹ 8 more rows

pivot_wider() does the opposite of pivot_longer(): one column per year again, so the result is easy to read. One row per country.

# A tibble: 4 × 4
  country_name `1970` `1990` `2010`
  <chr>         <dbl>  <dbl>  <dbl>
1 Burkina Faso   39.5   49.3   56.3
2 Honduras       54.4   65.2   69.8
3 Niger          35.9   41.7   57.1
4 Suriname       59.5   62.1   69.6

gt() turns the tibble into a formatted table, ready for a report.

country_name 1970 1990 2010
Burkina Faso 39.5 49.3 56.3
Honduras 54.4 65.2 69.8
Niger 35.9 41.7 57.1
Suriname 59.5 62.1 69.6

The completed notebook builds this pipeline one question at a time: Q3 pivot, Q4 parse, Q5 filter, Q6 pivot back, Q7 table.

Part 2: results

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
country_name 1970 1990 2010 gain
Niger 35.9 41.7 57.1 +21.2
Burkina Faso 39.5 49.3 56.3 +16.8
Honduras 54.4 65.2 69.8 +15.4
Suriname 59.5 62.1 69.6 +10.1

Life expectancy at birth, years. Source: World Bank, wb-life-exp.csv.

Workflow read → rename → pivot longer → parse → filter → pivot wider → table

Result Niger shows the largest gain (+21.2 years); most of it occurred after 1990

Iteration with purrr

Part 3

map() · bind_rows() · map_dfr()

Data stored as a list

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

split_data: a list of 4 data frames, one per cognitive domain

289inhibition
270processing speed
208shifting
501updating

Data 1,268 effect sizes from 431 studies on age differences in executive functioning

Common examples One file per year, one sheet per region, one table per survey wave

Iterating with map()

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
A list4 data frames
→
map(list, f)apply f to each element
→
A list4 results

A function by name

map(split_data, colnames)

Your own steps, with ~ and .x

map(split_data, ~ select(.x, author, yi, vi))

.x refers to the current list element.

Why not copy and paste?

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Copy and paste Four lines to type, four places to fix a typo, and it breaks when a fifth piece appears.

map() One line, whatever the number of pieces.

Combining results with bind_rows()

Setup1 · dplyr2 · tidyr3 · purrrWrap-up

Two steps

map(split_data,
    ~ .x |>
      as_tibble() |>
      select(author, yi, vi)) |>
  bind_rows(.id = "domain")

One step

map_dfr(split_data,
        ~ .x |>
          as_tibble() |>
          select(author, yi, vi),
        .id = "domain")

.id = "domain" Adds a column saying which piece each row came from

Newer purrr map_dfr() is superseded by map() |> list_rbind(), but you’ll still see it everywhere

Exercises: Part 3

Part 3 · notebook Q1–5

  1. Run the chunk that builds split_data (don’t worry how it works)
  2. map() with head(.x, 5): look at each piece
  3. map() with colnames, as_tibble, then select() four variables
  4. Combine 3. into one map() with a pipe inside
  5. bind_rows(.id = "domain") the result into one tibble
  6. Bonus: do steps 4 and 5 in one go with map_dfr()

Syntax you need

map(split_data, colnames)          # by name
map(split_data, ~ head(.x, 5))     # .x = each piece

map(split_data,
    ~ .x |>
      as_tibble() |>
      select(author, yi, vi)) |>
  bind_rows(.id = "domain")        # back to one table

Stuck?

  • object ‘.x’ not found: you forgot the ~
  • select() fails on one piece? Each piece has different columns. Check with map(split_data, colnames)
  • Lost track of the domain? Add .id = "domain"

Solutions: Part 3 · Q3

map(split_data, colnames)

Apply the same function to each of the four data frames in the list.

map() runs colnames() on each element of split_data and returns a list with one result per element. The pieces have different columns: only author, yi and vi appear in all four.

$inhibition
[1] "author" "yi"     "vi"     "task"  

$`processing speed`
[1] "author"    "yi"        "vi"       
[4] "n1"        "n2"        "mean_age1"
[7] "mean_age2"

$shifting
[1] "author" "yi"     "vi"     "task"  

$updating
[1] "author"    "yi"        "vi"       
[4] "n1"        "n2"        "mean_age1"
[7] "mean_age2"

Q2, map(split_data, ~ head(.x, 5)), prints the first five rows of each of the four data frames; too long for a slide.

Solutions: Part 3 · Q4–5

map(
  split_data,
  ~ .x |>
    as_tibble() |>
    select(author, yi, vi)
) |>
  bind_rows(.id = "domain")

Keep the same three columns in every piece of the list, then stack the pieces into one table.

For each piece, .x stands for that data frame: convert it to a tibble and keep three columns. The result is still a list of four tibbles.

$inhibition
# A tibble: 289 × 3
  author         yi    vi
  <chr>       <dbl> <dbl>
1 Bugg, 2014A  1.96  272.
2 Bugg, 2014B  3.39  164.
# ℹ 287 more rows

$`processing speed`
# A tibble: 270 × 3
  author           yi      vi
  <chr>         <dbl>   <dbl>
1 Balouch, 2013 1.53    0.980
2 Bernard, 2013 0.671 412.   
# ℹ 268 more rows

$shifting
# A tibble: 208 × 3
  author            yi         vi
  <chr>          <dbl>      <dbl>
1 Balouch, 2013 -0.258     0.0719
2 Barulli, 2013  2.73  34502.    
# ℹ 206 more rows

$updating
# A tibble: 501 × 3
  author           yi    vi
  <chr>         <dbl> <dbl>
1 Aguirre, 2014 0.871 2.29 
2 Balouch, 2013 0.396 0.114
# ℹ 499 more rows

bind_rows() stacks the four tibbles into one table with 1,268 rows. .id = "domain" adds a column recording which piece each row came from.

# A tibble: 1,268 × 4
  domain     author            yi     vi
  <chr>      <chr>          <dbl>  <dbl>
1 inhibition Bugg, 2014A     1.96 272.  
2 inhibition Bugg, 2014B     3.39 164.  
3 inhibition Bugg, 2014C     3.41 164.  
4 inhibition Circelli, 2013  1.08   1.26
# ℹ 1,264 more rows

Q5: map_dfr(split_data, ~ ..., .id = "domain") returns the same tibble in one step. Step 1 shows 2 rows per piece to fit the slide.

Summary

Setup1 · dplyr2 · tidyr3 · purrrWrap-up
1dplyr verbs (count, mutate, group_by, summarise) chained with |> cover most descriptive summaries
2Tidy data has one variable per column and one observation per row; pivot_longer() and pivot_wider() convert between formats
3map() applies a function to each element of a list; bind_rows() combines the results

Question: is the data you plan to use for your own project in tidy format? If not, which rule does it break?

Further reading

  • Finish any part you didn’t get to. The completed notebook goes up after class.
  • Read R for Data Science (2e): chapters on data transformation, data tidying and iteration
  • Posit’s two-page reference sheets for dplyr and tidyr list every function with a small example. Also under Help → Cheat Sheets in RStudio.

Week 2 Data visualisation with ggplot