typestar

The pipe in R

Chaining operations left to right instead of nesting.

library(dplyr)

result <- mtcars %>%
  filter(cyl == 4) %>%
  select(mpg, wt, hp) %>%
  arrange(desc(mpg)) %>%
  head(5)

native <- mtcars |>
  subset(mpg > 30) |>
  nrow()

How it works

  1. %>% passes the left value into the next call.
  2. Each verb reads as a step in one pipeline.
  3. |> is the native pipe with the same idea.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
160
Tokens
50
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 33 seconds.

Type this snippet

Step 4 of 4 in Tibbles & columns, step 4 of 27 in dplyr.

← Previous Next →