typestar

Per-group functions in R

group_modify hands each group to a function as a data frame.

library(dplyr)

runs <- tibble(
  lang = c("r", "r", "python", "python"),
  tpm = c(88, 96, 104, 99)
)

print(runs |>
        group_by(lang) |>
        group_modify(~ tibble(
          n = nrow(.x),
          span = diff(range(.x$tpm))
        )))

print(runs |> reframe(quantile = c(0.25, 0.75),
                      value = quantile(tpm, c(0.25, 0.75)), .by = lang))
print(group_split(group_by(runs, lang)) |> length())

How it works

  1. The function receives the group and its key.
  2. It must return a data frame, which is then stacked.
  3. reframe is the newer verb for many-row summaries.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
352
Tokens
123
Three-star pace
105 tpm

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

Type this snippet

Step 3 of 3 in Windows & rows, step 20 of 27 in dplyr.

← Previous Next →