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
- The function receives the group and its key.
- It must return a data frame, which is then stacked.
reframeis the newer verb for many-row summaries.
Keywords and builtins used here
cdiffgroup_bygroup_modifygroup_splitlengthlibrarynrowprintquantilerangereframetibble
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.
Step 3 of 3 in Windows & rows, step 20 of 27 in dplyr.