# ============================================================
# The Pipe (|>)
# R Foundations — Rverse Analytics
# https://rverseanalytics.com/learn/pipe
# ============================================================

library(dplyr)
# Nested calls read inside-out and are hard to follow
head(arrange(select(mtcars, mpg, hp), desc(mpg)))
# The native pipe |> feeds the left side into the next function
mtcars |> select(mpg, hp) |> arrange(desc(mpg)) |> head()
# A pipeline reads top-to-bottom, like a recipe
mtcars |>
  filter(cyl == 4) |>
  summarise(n = n(), avg_mpg = mean(mpg))
# The pipe works with base R too
c(4, 8, 15, 16, 23, 42) |> mean() |> round(1)
