Description Usage Arguments Floating point Examples
Bin a numeric vector and count how many observations fall in each bin. Supports weights so that you can re-bin pre-binned data.
1 2 3 4 5 6 7 |
x |
A numeric vector to guess parameters from. |
width |
(Positive real). The width of a bin. For S3 objects, the interpretation of width depends on the interpretation of the underlying numeric vector. For example, for dates, 1 = 1 day; for times 1 = 1 second; and for difftime, the units vary. If |
boundary, center |
Set the position of the first bin by specifying
the position of either a boundary or the center of a bin.
For example, you can always center the bins on integers with
Think of binning as tiling the real line into a infinite sequence of
intervals. |
origin, terminus |
The locations of the left-most and right-most bins.
Any values outside this range will be treated as missing. You should
usually leave |
bins |
Number of bins to use if not specified. Pretty bin sizes are preferred over matching this value exactly. |
pad |
If |
closed |
One of |
breaks |
A numeric vector of break points. |
If a point is less than binwidth
/ 10^8 from the boundary between
two bins, it is shifted to fall in the bin with the closest "closed" side.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | x <- runif(1e6)
compute_stat(bin_fixed(x), x)
compute_stat(bin_fixed(x, width = 0.25), x)
compute_stat(bin_breaks(c(0, 0.1, 0.9, 1)), x)
# Can also create fixed bins without data, if you supply the origin
# terminus, and width
bin_fixed(origin = 0, terminus = 1, width = 0.25)
bin_fixed(x, bins = 37)
# Bin other types of object
x1 <- Sys.time() + runif(1000) * 60
compute_stat(bin_date(x1), x1)
x2 <- Sys.Date() + sample(30, 10)
compute_stat(bin_date(x2), x2)
# For fixed bin width, performance scales linearly with the size of x.
x <- runif(1e7)
system.time(compute_stat(bin_fixed(x, width = 1e-1), x))
system.time(compute_stat(bin_fixed(x, width = 1e-2), x))
system.time(compute_stat(bin_fixed(x, width = 1e-5), x))
# For arbitrary breaks, performance scales linearly with x and
# logarthmically with the number of bins.
system.time(compute_stat(bin_breaks(seq(0, 1, length = 10)), x))
system.time(compute_stat(bin_breaks(seq(0, 1, length = 100)), x))
system.time(compute_stat(bin_breaks(seq(0, 1, length = 1000)), x))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.