Michael Mayer shows off the native R pipe:
What does the pipe do? It puts the object on its left as the first argument into the function on its right:
iris %>% head()
is a funny way of writinghead(iris
). It helps to avoid long function chains likef(g(h(x)))
, or repeated assignments.In 2021 and version 4.1, R has received its native forward pipe operator
|>
so that we can write nice code like this:
Tying pipe syntax all back together, the magrittr
pipe %>%
was (as I recall) built with the F# pipe |>
in mind. In R 4.1 and later, the built-in pipe is |>
, as is right and natural in this world. Regardless, do check the comment before trying out this code, as it appear to work for R 4.2 and later, though not 4.1.