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
.beforeand.aftertake a column name or a selection.- A selection helper moves a whole group at once.
selectwitheverything()does the same by listing.
Keywords and builtins used here
ceverythinglast_collibrarynamesprintrelocateselecttibblewhere
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.
Step 3 of 4 in Tibbles & columns, step 3 of 27 in dplyr.