bin_by_interval | R Documentation |
Groups a data frame (similarly to dplyr::group_by()
) based on the values of
a column, either by dividing up the range into equal pieces or by quantiles.
bin_by_interval(.data, col, breaks = NULL)
bin_by_quantile(.data, col, breaks = NULL)
.data |
Data frame to bin |
col |
Column to bin by |
breaks |
Number of bins to create. |
bin_by_interval()
breaks the numerical range of that column into
equal-sized intervals, or into intervals specified by breaks
.
bin_by_quantile()
splits the range into pieces based on quantiles of the
data, so each interval contains roughly an equal number of observations.
Grouped data frame, similar to those returned by dplyr::group_by()
.
An additional column .bin
indicates the bin number for each group. Use
dplyr::summarize()
to calculate values within each group, or other dplyr
operations that work on groups.
suppressMessages(library(dplyr))
cars |>
bin_by_interval(speed, breaks = 5) |>
summarize(mean_speed = mean(speed),
mean_dist = mean(dist))
cars |>
bin_by_quantile(speed, breaks = 5) |>
summarize(mean_speed = mean(speed),
mean_dist = mean(dist))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.