typestar

summarize in R

Collapsing many rows into one, or one per group.

library(dplyr)

runs <- tibble(
  lang = c("r", "r", "python", "python", "sql"),
  tpm = c(88, 96, 104, 99, 79),
  errors = c(3, 1, 0, 2, 5)
)

print(summarise(runs, runs = n(), mean_tpm = mean(tpm)))

print(summarise(runs,
                runs = n(),
                best = max(tpm),
                mean_tpm = round(mean(tpm), 1),
                clean = sum(errors == 0),
                .by = lang))

print(count(runs, lang, sort = TRUE))

How it works

  1. Each argument becomes a column of the result.
  2. n() counts rows in the group.
  3. .by groups for this call only, without a separate group_by.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
356
Tokens
136
Three-star pace
100 tpm

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

Type this snippet

Step 2 of 4 in Grouping, step 15 of 27 in dplyr.

← Previous Next →