Session 2: R Recap — the Tidyverse Way
Monday, 21 September 2026, 09:00–11:15 · Room MAD 099
What this session is about
Almost everything you ever do to a table is one of five operations: keep some rows, keep some columns, add a column, sort, or summarise. The dplyr package gives you one short function for each — filter(), select(), mutate(), arrange() and summarise() — and a way of chaining them together so that a pipeline reads like a sentence.
That is what this session is about. Once you have those six, a question like “what is the median price of four-star hotels within a kilometre of the centre?” becomes three or four lines of code that someone else can read.
We then use them to take apart the puzzle from session 1: hotels near the centre cost more, but the central ones also have more stars — so which is it?
We cover a whole semester’s worth of Statistik I in 135 minutes. That is deliberate: you have seen all of this before, so this is a refresher, not a first encounter, and it moves at refresher speed.
If it turns out that less of it stuck than you expected, that is normal and it is not a problem — but do something about it. There is no class on 5 and 12 October, and Report 1 will not take both weeks. Use the spare time to work through First steps in R (Gräbner-Radkowitsch 2026a) and, if the statistics is the shaky part, the statistics recap track (Gräbner-Radkowitsch 2026b).
Those two weeks are the last comfortable moment to close a gap. From session 5 the course stops revisiting the basics.
Statistik I and II taught you R in a different style — things like hotels$price[hotels$stars == 5]. Today is largely a different way of writing the same operations, one that keeps working when the question gets complicated. If the syntax looks unfamiliar, that is expected and it is not a gap in your knowledge.
The same two as session 1 — nothing new to install:
library(tidyverse)
library(flextable)If you skipped session 1 or never got them working: install.packages(c("tidyverse", "flextable")), once, in the Console.
Learning goals
- Distinguish the basic object types and know why the distinction causes trouble with real data
- Read a CSV file into R with
read_csv() - Use the five core
dplyrverbs:filter(),select(),mutate(),arrange(),summarise() - Combine
group_by()andsummarise()to compare groups - Chain steps with the pipe
|> - Turn a numeric code into a proper factor, and know why that matters
- Handle missing values honestly, and know why
na.rm = TRUEis not a fix - Recognise when a group comparison is misleading because the groups differ in more than one respect — or because a group is simply too small to compare
Slides
Open the slides in a separate tab
Open the slides in a separate tab in Chrome, add ?print-pdf to the end of the address, then print to PDF.
Materials
| File | Description |
|---|---|
| Exercise sheet | The in-session exercise |
| hotels-vienna.csv | The data, as in session 1 |
| Slides | The deck from the session |
| Live coding document | The narrated code we wrote together |
| Exemplary solution | Worked answers to the exercise |
| After the session | Questions that came up afterwards: Steps 9–11, rounding, mean vs. median, inline code, if_else() |
Data
The same Vienna hotels dataset as session 1. Sticking with one dataset for the first three sessions means you spend your attention on the code rather than on re-learning what the columns mean.
Békés and Kézdi (2021), licensed CC BY-NC-SA 4.0. Unmodified.
The question we answer
Session 1 ended with a puzzle: hotels near the centre of Vienna cost more, but the central ones also had more stars. So is the centre expensive because it is central, or because that is where the good hotels are?
Both, and the mix depends on the segment. Among three-star hotels the location premium is about €16 a night; among four-star hotels it is about €43. There is no single “location effect”.
For five-star hotels the question turns out to be unanswerable — every one of them is in the centre, so there is nothing to compare against.
Getting there needs two variables and four separate tables. Add a third variable and the approach collapses — which is the honest reason regression exists, and why we come back to this in session 8.
Reading for this session
Two short pieces on Method and Inquiry cover the object types this session leans on. Read them if the R side feels shaky — the second one covers factors, which we use in the live coding to turn star ratings into proper categories:
R object types III: factors and data frames (Gräbner-Radkowitsch 2026e)
About twenty minutes each. Neither is compulsory, but session 3 assumes you are comfortable with what is in them.
Take-home until session 3
- Finish the exercise sheet if you did not get through it — these pipelines come back
- Read Wickham et al. (2023), ch. 3 “Data transformation” — about 30 minutes
- Work through Gräbner-Radkowitsch (2026d) and Gräbner-Radkowitsch (2026e) if you have not already
- Print the dplyr cheat sheet and keep it next to your laptop
Session 3 is Quarto end to end, and Report 1 is issued at the end of it.
Going further
- Gräbner-Radkowitsch (2026c) — the first part of the object-types series, on functions; not needed today, but it is where the other two start from
- Wickham et al. (2023), ch. 3 “Data transformation”
- Wickham et al. (2023), ch. 16 “Factors”
- Ismay and Kim (2020), ch. 3 “Data wrangling”
| Verb | Question it answers |
|---|---|
filter() |
Which rows do I care about? |
select() |
Which columns do I care about? |
mutate() |
What do I need that is not there yet? |
arrange() |
What is at the top? |
group_by() + summarise() |
How does it differ across groups? |