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
completeadds the missing combinations of the columns you name.fillinside it supplies the values for the new rows.expand_gridbuilds every combination from scratch.
Keywords and builtins used here
anti_joinccompleteexpand_gridlibrarylistnrowprinttibble
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.
Step 3 of 3 in Nesting & gaps, step 8 of 12 in Reshaping & reading.