This is an enhanced version of base::cut()
that allows a custom
formatting to be applied to the values.
cut_format(x, breaks, include.lowest = FALSE, right = TRUE, ordered_result = FALSE, ..., format_fun = format, sep = ", ", paren = c("(", "[", ")", "]"))
x | a numeric vector which is to be converted to a factor by cutting. |
---|---|
breaks |
|
include.lowest | logical, indicating if an ‘x[i]’ equal to
the lowest (or highest, for |
right | logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa. |
ordered_result | logical: should the result be an ordered factor? |
... | Passed to |
format_fun |
|
sep |
|
paren |
|
http://stackoverflow.com/q/14456371/946850
cut_format(runif(10), seq(0, 1, by = 0.25), format_fun = function(x) paste(x * 100, "%"))#> [1] (0 %, 25 %] (75 %, 100 %] (50 %, 75 %] (0 %, 25 %] (0 %, 25 %] #> [6] (25 %, 50 %] (25 %, 50 %] (25 %, 50 %] (50 %, 75 %] (75 %, 100 %] #> Levels: (0 %, 25 %] (25 %, 50 %] (50 %, 75 %] (75 %, 100 %]cut_format(runif(10), seq(0, 1, by = 0.25), paren = c("<", "{", ">", "}"))#> [1] <0.75, 1.00} <0.00, 0.25} <0.00, 0.25} <0.25, 0.50} <0.25, 0.50} #> [6] <0.00, 0.25} <0.25, 0.50} <0.00, 0.25} <0.25, 0.50} <0.75, 1.00} #> Levels: <0.00, 0.25} <0.25, 0.50} <0.50, 0.75} <0.75, 1.00}