bin: Bin a variable

Description Usage Arguments Value See Also Examples

View source: R/bin.R

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.

See Also

cut, cut_number, quantile

Examples

 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)

dnegrey/miscTools documentation built on May 3, 2019, 2:57 p.m.