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
c()combines selections;!negates one.last_col()andnum_range()cover positions and patterns.all_oferrors on a missing name;any_ofdoes not.
Keywords and builtins used here
all_ofany_ofclast_collibrarynamesnum_rangeprintselectstarts_withtibble
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.
Step 3 of 3 in Reading & selecting, step 11 of 12 in Reshaping & reading.