typestar

mutate in R

Adding or replacing columns, computed from the ones already there.

library(dplyr)

runs <- tibble(chars = c(1820, 1980), seconds = c(60, 45))

print(mutate(runs,
             wpm = chars / 5 / (seconds / 60),
             fast = wpm > 80))

print(mutate(runs, cps = chars / seconds, .before = 1))
print(mutate(runs, wpm = chars / 5, .keep = "used"))
print(transmute(runs, minutes = seconds / 60))

How it works

  1. A new column can use a column defined earlier in the same call.
  2. .before and .after place it without a separate relocate.
  3. .keep controls which of the old columns survive.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
303
Tokens
99
Three-star pace
95 tpm

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

Type this snippet

Step 1 of 4 in New columns, step 10 of 27 in dplyr.

← Previous Next →