typestar

Tidy selection in R

The selection language shared by select, across, pivot_longer and the rest.

library(dplyr)
library(tidyr)

runs <- tibble(lang = "r", week_1 = 88, week_2 = 96, week_3 = 92, stars = 3L)

print(names(select(runs, c(lang, stars))))
print(names(select(runs, !starts_with("week"))))
print(names(select(runs, num_range("week_", 1:2))))
print(names(select(runs, last_col())))

wanted <- c("lang", "stars")
print(names(select(runs, all_of(wanted))))
print(names(select(runs, any_of(c("lang", "missing")))))

How it works

  1. c() combines selections; ! negates one.
  2. last_col() and num_range() cover positions and patterns.
  3. all_of errors on a missing name; any_of does not.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
422
Tokens
148
Three-star pace
100 tpm

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

Type this snippet

Step 3 of 3 in Reading & selecting, step 11 of 12 in Reshaping & reading.

← Previous Next →