Skip to content

Generate a sequence over the range of a vector

Usage

seq_range(x, n, by, trim = NULL, expand = NULL, pretty = FALSE)

Arguments

x

A numeric vector

n, by

Specify the output sequence either by supplying the length of the sequence with n, or the spacing between value with by. Specifying both is an error.

I recommend that you name these arguments in order to make it clear to the reader.

trim

Optionally, trim values off the tails. trim / 2 * length(x) values are removed from each tail.

expand

Optionally, expand the range by expand * (1 + range(x) (computed after trimming).

pretty

If TRUE, will generate a pretty sequence. If n is supplied, this will use pretty() instead of seq(). If by is supplied, it will round the first value to a multiple of by.

Examples

x <- rcauchy(100)
seq_range(x, n = 10)
#>  [1] -258.628312 -225.157725 -191.687139 -158.216553 -124.745967
#>  [6]  -91.275381  -57.804794  -24.334208    9.136378   42.606964
seq_range(x, n = 10, trim = 0.1)
#>  [1] -5.0341702 -3.6723146 -2.3104590 -0.9486034  0.4132522  1.7751079
#>  [7]  3.1369635  4.4988191  5.8606747  7.2225303
seq_range(x, by = 1, trim = 0.1)
#>  [1] -5.03417019 -4.03417019 -3.03417019 -2.03417019 -1.03417019
#>  [6] -0.03417019  0.96582981  1.96582981  2.96582981  3.96582981
#> [11]  4.96582981  5.96582981  6.96582981

# Make pretty sequences
y <- runif(100)
seq_range(y, n = 10)
#>  [1] 0.01792089 0.12533689 0.23275289 0.34016889 0.44758489 0.55500089
#>  [7] 0.66241689 0.76983289 0.87724889 0.98466489
seq_range(y, n = 10, pretty = TRUE)
#>  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
seq_range(y, n = 10, expand = 0.5, pretty = TRUE)
#>  [1] -0.4 -0.2  0.0  0.2  0.4  0.6  0.8  1.0  1.2  1.4

seq_range(y, by = 0.1)
#>  [1] 0.01792089 0.11792089 0.21792089 0.31792089 0.41792089 0.51792089
#>  [7] 0.61792089 0.71792089 0.81792089 0.91792089
seq_range(y, by = 0.1, pretty = TRUE)
#>  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0