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] -59.542807 -48.518955 -37.495103 -26.471252 -15.447400 -4.423548
#> [7] 6.600303 17.624155 28.648007 39.671858
seq_range(x, n = 10, trim = 0.1)
#> [1] -3.58894813 -2.36284623 -1.13674434 0.08935756 1.31545946
#> [6] 2.54156135 3.76766325 4.99376515 6.21986704 7.44596894
seq_range(x, by = 1, trim = 0.1)
#> [1] -3.5889481 -2.5889481 -1.5889481 -0.5889481 0.4110519 1.4110519
#> [7] 2.4110519 3.4110519 4.4110519 5.4110519 6.4110519 7.4110519
# Make pretty sequences
y <- runif(100)
seq_range(y, n = 10)
#> [1] 0.01080974 0.11995645 0.22910316 0.33824987 0.44739658 0.55654329
#> [7] 0.66569000 0.77483671 0.88398342 0.99313012
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.01080974 0.11080974 0.21080974 0.31080974 0.41080974 0.51080974
#> [7] 0.61080974 0.71080974 0.81080974 0.91080974
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