select in R
Choosing columns by name, position, pattern or type.
library(dplyr)
runs <- tibble(
lang = c("r", "python"),
tpm_best = c(96, 104),
tpm_mean = c(92, 99),
stars = c(3L, 3L)
)
print(select(runs, lang, stars))
print(select(runs, -stars))
print(select(runs, starts_with("tpm")))
print(select(runs, where(is.numeric)))
print(select(runs, stars, everything()))
print(rename(runs, best = tpm_best))
How it works
- A leading minus drops a column instead of keeping it.
starts_with,containsandwhereselect by pattern or predicate.everything()is how you move a column to the front.
Keywords and builtins used here
ceverythinglibraryprintrenameselectstarts_withtibblewhere
The run, in numbers
- Lines
- 15
- Characters to type
- 340
- Tokens
- 116
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 77 seconds.
Step 2 of 4 in Tibbles & columns, step 2 of 27 in dplyr.