sqrt(mean(c(1, 4, 9, 16, 25)))[1] 3.316625
Day 5 - Friday, May 22, 2026
Before class
Plan for today
Helpful links
Topics covered:
ggplot2 (reading code, not writing from scratch)When you finish, bring your quiz up to the front.
Remember this code from Wednesday?
This should be read from the inside-out:
c(1, 4, 9, 16, 25) makes a vector.mean(...) of that vector is 11.sqrt(11) is \(\approx\) 3.317.As the number of functions you nest grows, the harder and harder it gets to read.
Suppose we want to:
penguins data,body_mass_g is not missing,species,Written with nested function calls:
You have to read it from the inside out, and the arguments get separated from their functions. This is not ideal or intuitive for humans.
The pipe operator lets you read code left-to-right, top-to-bottom, which is the way you actually think about it.
Read this as:
“Take
penguins, then filter out missing values, then group by species, then summarize the average mass.”
That’s it. That’s the whole idea.
is exactly the same as
The pipe takes whatever’s on the left and inserts it as the first argument to the function on the right.
is the same as
The pipe always fills the first argument; the rest go inside the parentheses as usual.
It is very useful to chain pipes together to do many steps in order.
Compare to the nested version:
Here, we get the same answer. The piped version reads in the order you do the work.
Put each pipe step on its own line, with the |> at the end of the previous line.
Indent the continuation by 2 spaces.
Good:
Not so good (works, but much harder to read):
|> vs. %>%You will see two pipe operators in R code online:
| Pipe | Where it comes from | When introduced |
|---|---|---|
%>% |
The magrittr package (and tidyverse) |
2014 |
\|> |
Base R | 2021 |
They do almost the same thing. There are slight differences, but for the purposes of this course, the distinction is not very significant.
%>% came first. It’s the original tidyverse pipe from the magrittr package.|> later because the pipe became so popular they wanted it built in.%>%; I’ll mostly use |>.You can use either one. But try to be consistent within a single document.
You don’t have to type |> character-by-character. RStudio has a shortcut:
Cmd + Shift + MCtrl + Shift + MBy default, this shortcut inserts %>%. To make it insert |> instead:
Tools > Global Options... > Code > Use native pipe operator
You’ll see code in both styles all over the internet. Practice translating in your head.
Nested:
Piped:
This gives the same result, and is much easier to read the piped version.
It just rewrites your code. Each step still runs one at a time, in order.
+ in ggplot2
dplyr and other data verbs: use |> or %>%.ggplot2 layers: use + (it was developed before the pipe).You’ll see the pipe very often from here on out. Next time we start the dplyr verbs:
filter() - pick rowsselect() - pick columnsarrange() - sortmutate() - make new columnssummarize() - collapse to a single numbergroup_by() - split into groups before summarizingEvery one of these is designed to be chained together with the pipe.
To turn in: Save your script. We’ll check at the end of class.
Common things to watch for:
x |> then a new line - not \n |> f().penguins |> filter(penguins, ...): drop the first penguins.+ vs. |> again. dplyr verbs chain with |>; ggplot layers chain with +.f() not f. The pipe doesn’t change that.|> or %>%) takes the value on the left and feeds it as the first argument to the function on the right.|> and %>% are nearly interchangeable for our purposes.|>; ggplot layers chain with +.Tuesday: Data frames · intro to data graphics.
Upcoming Deadlines