
Chop into fixed-width intervals
Source:R/chop-by-width.R, R/breaks-by-width.R, R/tab.R
chop_width.Rdchop_width() chops x into intervals of fixed width.
Arguments
- x
A vector.
- width
Width of intervals.
- start
Starting point for intervals. By default the smallest finite
x(largest ifwidthis negative).- ...
Passed to
chop().- left
Logical. Left-closed or right-closed breaks?
Value
chop_* functions return a factor of the same length as x.
brk_* functions return a function to create breaks.
tab_* functions return a contingency table().
Details
If width is negative, chop_width() sets left = FALSE and intervals will
go downwards from start.
See also
Other chopping functions:
chop(),
chop_equally(),
chop_evenly(),
chop_fn(),
chop_mean_sd(),
chop_n(),
chop_proportions(),
chop_quantiles(),
chop_spikes(),
fillet()
Examples
chop_width(1:10, 2)
#> [1] [1, 3) [1, 3) [3, 5) [3, 5) [5, 7) [5, 7) [7, 9) [7, 9) [9, 11]
#> [10] [9, 11]
#> Levels: [1, 3) [3, 5) [5, 7) [7, 9) [9, 11]
chop_width(1:10, 2, start = 0)
#> [1] [0, 2) [2, 4) [2, 4) [4, 6) [4, 6) [6, 8) [6, 8) [8, 10] [8, 10]
#> [10] [8, 10]
#> Levels: [0, 2) [2, 4) [4, 6) [6, 8) [8, 10]
chop_width(1:9, -2)
#> [1] [1, 3] [1, 3] [1, 3] (3, 5] (3, 5] (5, 7] (5, 7] (7, 9] (7, 9]
#> Levels: [1, 3] (3, 5] (5, 7] (7, 9]
chop(1:10, brk_width(2, 0))
#> [1] [0, 2) [2, 4) [2, 4) [4, 6) [4, 6) [6, 8) [6, 8) [8, 10] [8, 10]
#> [10] [8, 10]
#> Levels: [0, 2) [2, 4) [4, 6) [6, 8) [8, 10]
tab_width(1:10, 2, start = 0)
#> [0, 2) [2, 4) [4, 6) [6, 8) [8, 10]
#> 1 2 2 2 3