typestar

Sequences and ordering in R

Generating and reordering regular vectors.

ones_to_ten <- 1:10
evens <- seq(2, 20, by = 2)
grid <- seq(0, 1, length.out = 5)

thrice <- rep(c("a", "b"), times = 3)
each_twice <- rep(1:3, each = 2)

reversed <- rev(ones_to_ten)
sorted <- sort(c(3, 1, 2), decreasing = TRUE)
positions <- order(c(30, 10, 20))

How it works

  1. 1:10 and seq build ranges by step or length.
  2. rep repeats values, times or each.
  3. sort reorders values; order returns positions.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
263
Tokens
94
Three-star pace
80 tpm

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

Type this snippet

Step 3 of 4 in Vectors & types, step 3 of 20 in Language basics.

← Previous Next →