# bin: Bin a variable In dnegrey/miscTools: R package with miscellaneous tools

## Description

`bin()` takes a vector `x` and returns a character vector of the same length, containing a binned version of `x`. Character, factor and logical vectors are coerced to character, while numeric vectors are binned according to parameter specification. This includes the number of desired bins, equally spaced vs. equally sized bins and the percentiles to force as the max/min endpoints of the low/high bins. `NA` values in `x` are returned as `NA` character values.

## Usage

 ```1 2 3 4 5``` ```bin(x, ...) ## S3 method for class 'numeric' bin(x, numBins = 10, equalBinSize = FALSE, minPct = 0, maxPct=100) ```

## Arguments

 `x` character, factor, logical or numeric vector `numBins` integer value >= 2; number of desired bins `equalBinSize` logical value; return equally sized (`TRUE`) or equally spaced (`FALSE`) bins `minPct` integer between 0 and 100 specifying a percentile to force as the max endpoint for the low (first) bin `maxPct` integer between 0 and 100 specifying a percentile to force as the min endpoint for the high (last) bin (must be > `minPct`)

## Value

A character vector containing binned values of `x`.

`cut, cut_number, quantile`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```# character, factor, logical bin(letters) bin(iris\$Species) bin(c(TRUE, FALSE, TRUE)) # numeric x <- round(rnorm(50, 100, 5)) print(x) bin(x) y <- factor(bin(x)) z <- as.numeric(substr(levels(y), 2, regexpr(",", levels(y)) - 1)) y <- factor(bin(x), levels = levels(y)[order(z)]) summary(y) ```