typestar

Reordering columns in R

relocate moves columns without listing every other one.

library(dplyr)

runs <- tibble(
  tpm = c(96, 104),
  accuracy = c(97.2, 99.1),
  lang = c("r", "python"),
  day = c(1, 2)
)

print(relocate(runs, lang))
print(relocate(runs, lang, day, .before = tpm))
print(relocate(runs, where(is.numeric), .after = last_col()))
print(names(select(runs, lang, everything())))

How it works

  1. .before and .after take a column name or a selection.
  2. A selection helper moves a whole group at once.
  3. select with everything() does the same by listing.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
302
Tokens
104
Three-star pace
90 tpm

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

Type this snippet

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

← Previous Next →