cut.ordered: Create Bins for ordered Factors

Description Usage Arguments Details Value Examples

View source: R/cut.ordered.R

Description

cut divides the range of x into intervals and codes the values in x according to the interval they fall into.

Usage

1
2
3
## S3 method for class 'ordered'
cut(x, breaks, labels = NULL, include.lowest = TRUE,
  right = TRUE, ordered_result = TRUE, label_sep = "-", ...)

Arguments

x

An ordered factor which is to be cut into ordered bins.

breaks

Either 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 x is to be cut.

labels

Labels for the levels of the resulting category. By default, labels are constructed using a-b, c-d interval notation. If labels = FALSE, simple integer codes are returned instead of a factor.

include.lowest

Logical, indicating if an x[i] equal to the lowest (or highest, for right = FALSE) breaks value should be included.

right

Logical, indicating how to create the bins. This is utilized in two different ways based on the type of breaks argument. In the conventional case, where a breaks vector is supplied, right = TRUE indicates that bins should be closed on the right (and open on the left) or vice versa. If a single integer breaks value is provided, then right = TRUE indicates that bins will be determined such that those on the right are larger (if it is not possible for all bins to be evenly sized).

ordered_result

Logical: should the result be an ordered factor? Note that since the input data is ordered this argument is TRUE by default.

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 a-c d-g i-z etc.

...

Further arguments to be passed to or from other methods, in particular to cut.default.

Details

Note that the dig.lab argument from cut.default, is replaced for cut.ordered by a new argument: label_sep. Also note that unlike cut.default, here include.lowest defaults to TRUE, since this is more intuitive for the class ordered. Finally, since the input for cut.ordered is of class ordered, this is the default output as well. This contrasts with cut.default, which produces an unordered output by default.

Value

A factor is returned, unless labels = FALSE which results in an integer vector of level codes.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 some_letters <- cfactor(letters, ordered = TRUE)
 # bin letters into two groups
 cut(some_letters, breaks = c("a", "q", "z"), 
     labels = c("beginning of the alphabet", "the rest of the alphabet"), 
     right = TRUE, include.lowest = TRUE)
 # alter separator
 cut(some_letters, breaks = 20, label_sep = " to ")
 ## Not run: 
 # warnings
 ## missing values created
 cut(some_letters, breaks = c("a", "g"), label_sep = " to ")
 ## bins of width 1
 cut(some_letters, breaks = 20)
     
## End(Not run)

jonmcalder/refactor documentation built on Nov. 16, 2020, 3:46 a.m.