bins: Cut Numeric Values Into Evenly Distributed Groups (Bins)

Description Usage Arguments Details Value See Also Examples

View source: R/binning.R

Description

bins - Cuts points in vector x into evenly distributed groups (bins). bins takes 3 separate approaches to generating the cuts, picks the one resulting in the least mean square deviation from the ideal cut - length(x) / target.bins points in each bin - and then merges small bins unless excat.groups is TRUE

Usage

1
bins(x, ...)

Arguments

x

Vector of numbers

target.bins

Number of groups desired; this is also the max number of groups.

max.breaks

Used for initial cut. If exact.groups is FALSE, bins are merged until there's no bins with fewer than length(x) / max.breaks points. In bins, one of max.breaks and minpts must be supplied.

exact.groups

if TRUE, the result will have exactly the number of target.bins; if FALSE, the result may contain fewer than target.bins bins

verbose

Indicates verbose output.

errthresh

If the error is below the provided value, stops after the first rough estimate of the bins.

minpts

Minimum number of points in a bin. In bins, one of max.breaks and minpts must be supplied.

Details

The gains are computed using incremental analytical expresions derived for moving a value from one bin to the next, splitting a bin into two or merging two bins.

Value

A list containing the following items (not all of them may be present):

See Also

binr

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
   # Seriously skewed x:
   x <- floor(exp(rnorm(200000 * 1.3)))
   cuts <- bins(x, target.bins = 10, minpts = 2000)
   cuts$breaks <- bins.getvals(cuts)
   cuts$binct
   #   [0, 0]    [1, 1]    [2, 2]    [3, 3]    [4, 4]    [5, 5]    [6, 7]   [8, 10]
   # 129868     66611     28039     13757      7595      4550      4623      2791
   #   [11, 199]
   # 2166

   # Centered x:
   x <- rep(c(1:10,20,31:40), c(rep(1, 10), 100, rep(1,10)))
   cuts <- bins(x, target.bins = 3, minpts = 10)
   cuts$binct
   # [1, 10] [20, 20] [31, 40]
   #      10      100       10

## End(Not run)

smsaladi/myutils documentation built on Jan. 11, 2020, 2:07 a.m.