bin_cols | R Documentation |
Make bins in a tidy fashion. Adds a column to your data frame containing the integer codes of the specified bins of a certain column. Specifying multiple columns is only intended for supervised binning, so mutliple columns can be simultaneously binned optimally with respect to a target variable.
bin_cols(
.data,
col,
n_bins = 10,
bin_type = "frequency",
...,
target = NULL,
pretty_labels = FALSE,
seed = 1,
method = "mdlp"
)
.data |
a data frame |
col |
a column, vector of columns, or tidyselect |
n_bins |
number of bins |
bin_type |
method to make bins |
... |
params to be passed to selected binning method |
target |
unquoted column for supervised binning |
pretty_labels |
logical. If T returns interval label rather than integer rank |
seed |
seed for stochastic binning (xgboost) |
method |
method for bin mdlp |
Description of the arguments for bin_type
creates bins of equal content via quantiles. Wraps bin
with method "content". Similar to ntile
create bins of equal numeric width. Wraps bin
with method "length"
create bins using 1-dimensional kmeans. Wraps bin
with method "clusters"
each bin has equal sum of values
column is binned by best predictor of a target column using step_discretize_xgb
if the col does not have enough distinct values, xgboost will fail and automatically revert to step_discretize_cart
column is binned by weight of evidence. Requires binary target
column is binned by logistic regression. Requires binary target.
uses the discretizeDF.supervised
algorithm with a variety of methods.
a data frame
iris %>%
bin_cols(Sepal.Width, n_bins = 5, pretty_labels = TRUE) %>%
bin_cols(Petal.Width, n_bins = 3, bin_type = c("width", "kmeans")) %>%
bin_cols(Sepal.Width, bin_type = "xgboost", target = Species, seed = 1) -> iris1
#binned columns are named by original name + method abbreviation + number bins created.
#Sometimes the actual number of bins is less than n_bins if the col lacks enough variance.
iris1 %>%
print(width = Inf)
iris1 %>%
bin_summary() %>%
print(width = Inf)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.