This Wrappers creates partial functions with x as sole argument of the ggplot2 cut functions
Usage
cut_interval.fun(n = NULL, length = NULL, ...)
cut_number.fun(n = NULL, ...)
cut_width.fun(
width,
center = NULL,
boundary = NULL,
closed = c("right", "left"),
...
)Arguments
- n
number of intervals to create, OR
- length
length of each interval
- ...
Arguments passed on to
base::cut.defaultbreakseither a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which
xis to be cut.labelslabels for the levels of the resulting category. By default, labels are constructed using
"(a,b]"interval notation. Iflabels = FALSE, simple integer codes are returned instead of a factor.rightlogical, indicating if the intervals should be closed on the right (and open on the left) or vice versa.
dig.labinteger which is used when labels are not given. It determines the number of digits used in formatting the break numbers.
ordered_resultlogical: should the result be an ordered factor?
- width
The bin width.
- center, boundary
Specify either the position of edge or the center of a bin. Since all bins are aligned, specifying the position of a single bin (which doesn't need to be in the range of the data) affects the location of all bins. If not specified, uses the "tile layers algorithm", and sets the boundary to half of the binwidth.
To center on integers,
width = 1andcenter = 0.boundary = 0.5.- closed
One of
"right"or"left"indicating whether right or left edges of bins are included in the bin.
Examples
data <- tibble::tibble(x = runif(100, 0, 10))
funs <- list(
interval = cut_interval.fun(n = 5),
number = cut_number.fun(n = 5),
width = cut_width.fun(width = 2, boundary = 0)
)
res <- dplyr::mutate_all(tibble::tibble(x = runif(100, 0, 10)), funs)
res
#> # A tibble: 100 × 4
#> x interval number width
#> <dbl> <fct> <fct> <fct>
#> 1 6.49 (5.94,7.89] (5.16,6.52] (6,8]
#> 2 9.85 (7.89,9.85] (8.13,9.85] (8,10]
#> 3 5.36 (3.99,5.94] (5.16,6.52] (4,6]
#> 4 7.61 (5.94,7.89] (6.52,8.13] (6,8]
#> 5 5.64 (3.99,5.94] (5.16,6.52] (4,6]
#> 6 7.78 (5.94,7.89] (6.52,8.13] (6,8]
#> 7 6.39 (5.94,7.89] (5.16,6.52] (6,8]
#> 8 3.79 (2.04,3.99] (2.16,5.16] (2,4]
#> 9 3.60 (2.04,3.99] (2.16,5.16] (2,4]
#> 10 8.44 (7.89,9.85] (8.13,9.85] (8,10]
#> # ℹ 90 more rows
table(res$interval)
#>
#> [0.0866,2.04] (2.04,3.99] (3.99,5.94] (5.94,7.89] (7.89,9.85]
#> 20 11 19 29 21
table(res$number)
#>
#> [0.0866,2.16] (2.16,5.16] (5.16,6.52] (6.52,8.13] (8.13,9.85]
#> 20 20 20 20 20
table(res$width)
#>
#> [0,2] (2,4] (4,6] (6,8] (8,10]
#> 20 11 19 29 21
