[1] "data.frame"
MY202 · Lab 1
Jonas Weinert
Department of Methodology | London School of Economics and Political Science
1 October 2026
| 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.
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.
Before we start
Objects · functions · vectors · data frames · lists
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().
c(39.5, 49.3, 56.3)
Several values of one type in a sequence. c() combines values into a vector.
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(df1, x, df2)
A container whose elements can be any kind of object, of any length. Used in Part 3.
| 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 ==filter(year = 1990)filter(year == 1990)= 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 `==`.
Part 1
count() · mutate() · group_by() · summarise()
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().
|>Keyboard shortcut: Ctrl + Shift + M (Windows), Cmd + Shift + M (Mac).
dplyr verbscount 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.
group_by()Part 1 · notebook Q1–11
?agadullina2018: read the documentationclass(): what kind of object is it?as_tibble() with a new name, then printcount() the frequencies of design|>mutate(): add a percentage columnsummarise() the mean of yigroup_by(design) before summarise()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 eachStuck?
<-?group_by()glimpse(), or filter(n > 200) firstFind 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>
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.
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.
How many effect sizes per design?
Mean effect size (yi)
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.
Part 2
pivot_longer() · pivot_wider() · parse_number()
Illustration: Julia Lowndes and Allison Horst, Tidy Data for reproducibility, efficiency, and collaboration, Openscapes blog, 2020. CC BY 4.0.
Illustration: Julia Lowndes and Allison Horst, Tidy Data for reproducibility, efficiency, and collaboration, Openscapes blog, 2020. CC BY 4.0.
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.
pivot_longer() and pivot_wider()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
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.
Part 2 · notebook Q1–8
read_csv() the file, rename() column 1 to country_namepull() the countries: spot the non-countriespivot_longer() everything except country_nameparse_number() the year columnfilter() four countries in 1970, 1990, 2010pivot_wider() back to one column per yeargt() it into a formatted tableSyntax 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?
data/ folder?%in% with a vector, not ==pull()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.
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.
| 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
Part 3
map() · bind_rows() · map_dfr()
split_data: a list of 4 data frames, one per cognitive domain
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
map()map(list, f)apply f to each element.x refers to the current list element.
select(split_data$inhibition, author, yi, vi)
select(split_data$shifting, author, yi, vi)
select(split_data$updating, author, yi, vi)
select(split_data$`processing speed`, ...)map(split_data, ~ select(.x, author, yi, vi))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.
bind_rows()Two steps
.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
Part 3 · notebook Q1–5
split_data (don’t worry how it works)map() with head(.x, 5): look at each piecemap() with colnames, as_tibble, then select() four variablesmap() with a pipe insidebind_rows(.id = "domain") the result into one tibblemap_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 tableStuck?
~select() fails on one piece? Each piece has different columns. Check with map(split_data, colnames).id = "domain"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.
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.
dplyr verbs (count, mutate, group_by, summarise) chained with |> cover most descriptive summariespivot_longer() and pivot_wider() convert between formatsmap() applies a function to each element of a list; bind_rows() combines the resultsQuestion: is the data you plan to use for your own project in tidy format? If not, which rule does it break?
Week 2 Data visualisation with ggplot
MY202 · Lab 1 · Tidyverse