typestar

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

  1. A leading minus drops a column instead of keeping it.
  2. starts_with, contains and where select by pattern or predicate.
  3. everything() is how you move a column to the front.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 4 in Tibbles & columns, step 2 of 27 in dplyr.

← Previous Next →

select in other languages