Applied Data Science - Session 2
Europa-Universität Flensburg
2026-09-21
So: exercises done, everything clear.
Post what is genuinely open
Dragged-along problems accumulate — and end in frustration.
Please complete the survey this week
Too few responses — I cannot see where to start.
Is the centre expensive because it is central — or because that is where the good hotels are?
Five verbs at the bottom of 90% of data preparation!
“Everything that exists is an object, everything that happens is a function call”.
– John Chambers
| Type | Example | What it is |
|---|---|---|
double |
240, 3.5, 7.0 |
decimal numbers |
integer |
7L, 120L |
whole numbers |
character |
"Hotel Sacher" |
text, always in quotes |
logical |
TRUE, FALSE |
yes/no |
typeof() tells you what is stored in memory, class() the high-level type.
"240" — text that looks like a numbern/a in an Excel export → whole column arrives as textc()) vectors: all elements are of the same typeVectorization
One operation, every element at once — no loop.
lists: R needs to guess type for each of its elementsdata frame: rows = observations, columns = variables
read_csv() — comma-separated files, the common caseread_csv2() — semicolons and decimal commas, i.e. German Excel exportsreadxl::read_excel() — .xlsx directlypackage::function()
Reaches into a package without library(). Worth it for one function used once.
Look before you use it
glimpse() — ninety percent of import bugs show in the first three rows.
# A tibble: 8 × 2
stars n
<dbl> <int>
1 1 1
2 2 47
3 2.5 5
4 3 140
5 3.5 57
6 4 143
7 4.5 8
8 5 27
stars is a number → R will report a mean of 3.43# A tibble: 5 × 2
star_tier n
<fct> <int>
1 1 stars 1
2 2 stars 47
3 3 stars 145
4 4 stars 200
5 5 stars 35
Eight ragged groups become five.
round() does not do what you thinkBanker’s rounding
Halves go to the nearest even number. Silent, and inconsistent for binning.
floor(x + 0.5) rounds halves up, every time.
Almost everything you do to a table is one of five things:
| Verb | What it does |
|---|---|
select() |
keep some columns |
filter() |
keep some rows |
mutate() |
add a new column |
arrange() |
sort the rows |
summarise() |
collapse rows to a single value |
Plus group_by(), which makes summarise() and mutate() work per group.
select() — keep some columns# A tibble: 428 × 4
hotel_id price distance stars
<dbl> <dbl> <dbl> <dbl>
1 21894 81 2.7 4
2 21897 81 1.7 4
3 21901 85 1.4 4
4 21902 83 1.7 3
5 21903 82 1.2 4
6 21904 229 0.9 5
7 21906 103 0.9 4
8 21907 150 1 4
9 21908 80 0.7 2
10 21910 153 1.5 3
# ℹ 418 more rows
Four columns out of 24. Nothing is deleted — the result is a new table.
filter() — keep some rows# A tibble: 27 × 4
hotel_id price distance stars
<dbl> <dbl> <dbl> <dbl>
1 21904 229 0.9 5
2 21967 299 0.1 5
3 21969 513 0.3 5
4 21976 806 0.3 5
5 21989 261 0.3 5
6 21999 203 0.3 5
7 22006 544 0.5 5
8 22007 252 0.5 5
9 22008 323 0.5 5
10 22020 200 0.4 5
# ℹ 17 more rows
== equal · != not equal · > < >= <= · , and · | or
= is not ==
= assigns, == compares. Mixing them up is a rite of passage.
|>Two steps cost us a junk object, hotels_small. Two ways to avoid it.
Pipe — and read top to bottom:
Read |> as “and then”. Shortcut: Ctrl/Cmd + Shift + M.
mutate() — add a column# A tibble: 428 × 3
price stars price_per_star
<dbl> <dbl> <dbl>
1 81 4 20.2
2 81 4 20.2
3 85 4 21.2
4 83 3 27.7
5 82 4 20.5
6 229 5 45.8
7 103 4 25.8
8 150 4 37.5
9 80 2 40
10 153 3 51
# ℹ 418 more rows
Nothing is lost
Computed row by row — nothing is overwritten unless you reuse a name.
This is also how star_tier becomes a column.
Only a factor can bring them back
A category with zero observations looks exactly like one you never had.
group_by() + summarise()# A tibble: 5 × 3
star_tier n median_price
<fct> <int> <dbl>
1 1 stars 1 81
2 2 stars 14 66.5
3 3 stars 84 84
4 4 stars 125 115
5 5 stars 29 226
Nine tenths of business reporting is a group_by() then a summarise()
Take the hotels, and then keep only the ones actually in Vienna, and then group by type of accommodation, and then work out the median price in each group, and then sort from most to least expensive.
If you can say it, you can write it.
NA: we do not know.na.rm = TRUE
Discards, does not fix. Fine if missing at random. Always report how many you dropped.
How much more can a central five-star hotel charge?
# A tibble: 4 × 3
band n median_price
<fct> <int> <dbl>
1 <1 km 25 252
2 1-2 km 2 114.
3 2-3 km 2 128
4 >3 km 0 NA
Look at n before you look at the price.
Always print n next to your summary statistic
A median from two observations looks exactly like a median from two hundred.
We answer the opening question together:
Step by step
Build pipelines line by line. Run, look, add the next line.
Four business questions about the Vienna hotel market, answered with the five verbs — then one question of your own.
Write it up as a Quarto report with a chart at the end.
dplyr cheat sheet and keep it next to your laptopSession 3: Quarto properly — and Report 1 is issued at the end of it.
Applied Data Science 2026 | Session 2