Session 2: R Recap — the Tidyverse Way

Published

21 09 2026

Modified

08 10 2026

Note

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?

WarningThis is a lot of ground for one session

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.

NoteYou have used R before

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.

NotePackages you need today

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 dplyr verbs: filter(), select(), mutate(), arrange(), summarise()
  • Combine group_by() and summarise() 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 = TRUE is 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 II: vectors (Gräbner-Radkowitsch 2026d)

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

Important
  1. Finish the exercise sheet if you did not get through it — these pipelines come back
  2. Read Wickham et al. (2023), ch. 3 “Data transformation” — about 30 minutes
  3. Work through Gräbner-Radkowitsch (2026d) and Gräbner-Radkowitsch (2026e) if you have not already
  4. 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

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?

References

Békés, G. and Kézdi, G. (2021) Data analysis for business, economics, and policy, Cambridge: Cambridge University Press, available at https://gabors-data-analysis.com/.
Gräbner-Radkowitsch, C. (2026a) First steps in R, 16 June 2026, Method and Inquiry, available at https://euf-methodology-hub.netlify.app/resources/first-steps-in-r (accessed August 2026).
Gräbner-Radkowitsch, C. (2026b) Learning track: A recap of undergraduate statistics, 14 June 2026, Method and Inquiry, available at https://euf-methodology-hub.netlify.app/tracks/statistics-recap (accessed August 2026).
Gräbner-Radkowitsch, C. (2026c) R object types I: functions, 16 June 2026, Method and Inquiry, available at https://euf-methodology-hub.netlify.app/resources/r-object-types-i-functions (accessed September 2026).
Gräbner-Radkowitsch, C. (2026d) R object types II: vectors, 16 June 2026, Method and Inquiry, available at https://euf-methodology-hub.netlify.app/resources/r-object-types-ii-vectors (accessed August 2026).
Gräbner-Radkowitsch, C. (2026e) R object types III: Factors and data frames, 16 June 2026, Method and Inquiry, available at https://euf-methodology-hub.netlify.app/resources/r-object-types-iii-factors-dataframes (accessed August 2026).
Ismay, C. and Kim, A. Y.-S. (2020) Statistical inference via data science: A ModernDive into r and the tidyverse, Boca Raton: CRC Press, Taylor; Francis Group, available at https://moderndive.com/.
Wickham, H., Çetinkaya-Rundel, M. and Grolemund, G. (2023) R for data science: Import, tidy, transform, visualize, and model data, 2nd edition., Beijing et al.: O’Reilly, available at https://r4ds.hadley.nz/.