Description Usage Arguments Details Value Examples
cut divides the range of x
into intervals and codes the values in x
according
to the interval they fall into.
1 2 3 4 |
x |
A numeric vector which is to be converted to a factor by cutting. |
breaks |
Either an integer vector of two or more unique cut points or a
single integer (greater than or equal to 2) giving the number of intervals
into which |
labels |
Labels for the levels of the resulting category. By default,
labels are constructed using "a-b c-d" interval notation. If
|
include.lowest |
Logical, indicating if an "x[i]" equal to the lowest
(or highest, for |
right |
Logical, indicating how to create the bins. This is utilized in
different ways based on the type of breaks argument. In the conventional
case, where a breaks vector is supplied, |
ordered_result |
Logical: should the result be an ordered factor? |
breaks_mode |
A parameter indicating how to determine the intervals when breaks is specified as a scalar (note that this argument has no effect if breaks is specified as a vector). Can be 'default', 'spread' or 'pretty'. See 'Details' below. |
label_sep |
A single or short character string used to generate labels for the intervals e.g. the default value of "-" will result in labels like 1-10 11-20 21-30 etc. |
... |
Further arguments to be passed to or from other methods,
in particular to |
In deviation from cut.default
, cut.integer
does not
have an argument dig.lab
, but instead has two arguments that do not
exist for cut.default
: breaks_mode
and label_sep
.
Note that unlike cut.default
, here
include.lowest
defaults to TRUE
, since this is more intuitive
for the class integer
.
If breaks
is supplied as a scalar, the value of breaks_mode
determines
how the breaks are constructed:
'default' will produce intervals which are the (integer) equivalent
to those produced by cut.default
i.e. the bins/groupings will be the
same - but the labels will be of the form int-int/2-4 instead of
(numeric, numeric]/(1.5,4.2].
'spread' will result in intervals spread as evenly as possible
over the exact range of x
. If the intervals cannot all be equal,
then right
determines whether the rightmost (TRUE
) or
leftmost (FALSE
) intervals are made slightly wider.
'pretty' will generate rounded breakpoints for the intervals based
on pretty
. Note that breaks here is treated as the
'desired' number of intervals and is not guaranteed. Note also that the
range of x
can be exceeded slightly by the intervals in some
cases.
A factor is returned, unless labels = FALSE
which results in
an integer vector of level codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | random <- sample(11)
# nice labels compared to default
cut(random, breaks = seq(0, 100, by = 10))[1:10]
cut.default(random, breaks = seq(0, 100, by = 10))[1:10]
cut(random, breaks = 2) # create two bins
cut(sample(10), breaks = c(1, 6, 8, 9, 10)) # nice labels if binwidth is one
cut(random, breaks = 2, label_sep = " to ") # other label separator
cut(random, breaks = c(1, 8, 11), right = FALSE) # bins closed on right
# if uneven, make right bin slightly larger
cut(random, breaks = 2, breaks_mode = "spread", right = TRUE)
## Not run:
# warnings
cut(sample(10), breaks = c(0, 3, 5)) # missing values
cut(sample(10), breaks = c(10, 0, 3)) # unordered breaks
cut(sample(10), breaks = c(1, 2.6, 5.1, 10)) # non-integer breakpoints
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.