Generate a sequence over the range of a vector
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 withby
. 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. Ifn
is supplied, this will usepretty()
instead ofseq()
. Ifby
is supplied, it will round the first value to a multiple ofby
.
Examples
x <- rcauchy(100)
seq_range(x, n = 10)
#> [1] -69.606705 -50.710825 -31.814944 -12.919064 5.976817 24.872698
#> [7] 43.768578 62.664459 81.560340 100.456220
seq_range(x, n = 10, trim = 0.1)
#> [1] -9.12235862 -7.61545533 -6.10855205 -4.60164876 -3.09474548
#> [6] -1.58784219 -0.08093891 1.42596438 2.93286766 4.43977095
seq_range(x, by = 1, trim = 0.1)
#> [1] -9.1223586 -8.1223586 -7.1223586 -6.1223586 -5.1223586 -4.1223586
#> [7] -3.1223586 -2.1223586 -1.1223586 -0.1223586 0.8776414 1.8776414
#> [13] 2.8776414 3.8776414
# Make pretty sequences
y <- runif(100)
seq_range(y, n = 10)
#> [1] 0.01139899 0.12047311 0.22954724 0.33862136 0.44769548 0.55676960
#> [7] 0.66584373 0.77491785 0.88399197 0.99306610
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.01139899 0.11139899 0.21139899 0.31139899 0.41139899 0.51139899
#> [7] 0.61139899 0.71139899 0.81139899 0.91139899
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