Learn R Programming

mvbutils (version 2.5.4)

mcut: Put reals and integers into specified bins, returning factors.

Description

Put reals and integers into specified bins, returning factors. Like cut but for human use.

Usage

mcut( x, breaks, pre.lab='', mid.lab='', post.lab='', digits=getOption( 'digits'))
mintcut( x, breaks, prefix='', all.levels=)

Arguments

x
(numeric vector) What to bin-- will be coerced to integer for mintcut
breaks
(numeric vector) LH end of each bin-- should be increasing. Values of x exactly on the LH end of a bin will go into that bin, not the previous one. Should start with -Inf if necessary, but should not finish with Inf unless you want a bin for
prefix, pre.lab
(string) What to prepend to the factor labels-- e.g. "Amps" if your original data is about Amps.
mid.lab
"units" to append to numeric vals inside factor labels. Tends to make the labels harder to read; try using post.lab instead.
post.lab
(string) What to append to the factor labels.
digits
(integer) How many digits to put into the factor labels.
all.levels
if FALSE, omit factor levels that don't occur in x. To override "automatically", just set the "all.levels" attribute of breaks to anything non-NULL; useful e.g. if you are repeatedly calling mintcut with the same

Details

Values of x below breaks[1] will end up as NAs. For mintcut, factor labels (well, the bit after the prefix) will be of the form "2-7" or "3" (if the bin range is 1) or "8+" (for last in range). For mcut, labels will look like this (apart from the pre.lab and post.lab bits): "[<0.25]" or="" "[0.25,0.50]"="" "[="">=0.75]".

Examples

Run this code
set.seed( 1)
mcut( runif( 5), c( 0.25, 0.5, 0.75))
# [1] [0.25,0.50] [0.25,0.50] [0.50,0.75] [>=0.75]     [<0.25]
# Levels: [<0.25] [0.25,0.50] [0.50,0.75] [>=0.75]
 mcut( runif( 5), c( 0.25, 0.5, 0.75), pre.lab='A', post.lab='B', digits=1)
# [1] A[>=0.8]B    A[>=0.8]B    A[0.5,0.8]B A[0.5,0.8]B A[<0.2]B
# Levels: A[<0.2]B A[0.2,0.5]B A[0.5,0.8]B A[>=0.8]B
mintcut( 1:8, c( 2, 4, 7))
# [1] <NA> 2-3  2-3  4-6  4-6  4-6  7+   7+
# Levels: 2-3 4-6 7+

Run the code above in your browser using DataLab