typestar

Filling in the gaps in R

complete and expand_grid create the rows that should exist but do not.

library(tidyr)
library(dplyr)

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

print(complete(runs, lang, day = 1:3, fill = list(tpm = 0)))
print(expand_grid(lang = c("r", "python"), day = 1:2))
print(nrow(complete(runs, lang, day = 1:3)))
print(anti_join(expand_grid(lang = c("r", "python"), day = 1:3), runs,
                by = c("lang", "day")))

How it works

  1. complete adds the missing combinations of the columns you name.
  2. fill inside it supplies the values for the new rows.
  3. expand_grid builds every combination from scratch.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
374
Tokens
150
Three-star pace
105 tpm

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

Type this snippet

Step 3 of 3 in Nesting & gaps, step 8 of 12 in Reshaping & reading.

← Previous Next →