Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 54 lines (48 sloc) 1.597 kb
036110c @hadley Document and export cut_interval and cut_number
authored
1 #' Cut numeric vector into intervals of equal length.
1244716 @hadley Trim whitespace
authored
2 #'
036110c @hadley Document and export cut_interval and cut_number
authored
3 #' @param x numeric vector
4 #' @param n number of intervals to create, OR
5 #' @param length length of each interval
6 #' @param ... other arguments passed on to \code{\link{cut}}
7 #' @seealso \code{\link{cut_number}}
8 #' @export
1244716 @hadley Trim whitespace
authored
9 #' @examples
036110c @hadley Document and export cut_interval and cut_number
authored
10 #' table(cut_interval(1:100, n = 10))
11 #' table(cut_interval(1:100, n = 11))
12 #' table(cut_interval(1:100, length = 10))
b0f5d2f @hadley Final tweaks prior to release.
authored
13 cut_interval <- function(x, n = NULL, length = NULL, ...) {
14 cut(x, breaks(x, "width", n, length), include.lowest = TRUE, ...)
15 }
16
036110c @hadley Document and export cut_interval and cut_number
authored
17 #' Cut numeric vector into intervals containing equal number of points.
1244716 @hadley Trim whitespace
authored
18 #'
036110c @hadley Document and export cut_interval and cut_number
authored
19 #' @param x numeric vector
20 #' @param n number of intervals to create
21 #' @param ... other arguments passed on to \code{\link{cut}}
22 #' @seealso \code{\link{cut_interval}}
23 #' @export
24 #' @examples
25 #' table(cut_number(runif(1000), n = 10))
b0f5d2f @hadley Final tweaks prior to release.
authored
26 cut_number <- function(x, n = NULL, ...) {
27 cut(x, breaks(x, "n", n), include.lowest = TRUE, ...)
28 }
29
da4b8cb @hadley New discretising functions to replace chop
authored
30 breaks <- function(x, equal, nbins = NULL, binwidth = NULL) {
31 equal <- match.arg(equal, c("numbers", "width"))
87bc512 @hadley Rename params
authored
32 if ((!is.null(nbins) && !is.null(binwidth)) || (is.null(nbins) && is.null(binwidth))) {
33 stop("Specify exactly one of n and width")
34 }
1244716 @hadley Trim whitespace
authored
35
da4b8cb @hadley New discretising functions to replace chop
authored
36 rng <- range(x, na.rm = TRUE, finite = TRUE)
37 if (equal == "width") {
38 if (!is.null(binwidth)) {
39 fullseq(rng, binwidth)
40 } else {
41 seq(rng[1], rng[2], length = nbins + 1)
42 }
43 } else {
44 if (!is.null(binwidth)) {
45 probs <- seq(0, 1, by = binwidth)
46 } else {
47 probs <- seq(0, 1, length = nbins + 1)
48 }
49 quantile(x, probs, na.rm = TRUE)
50 }
1244716 @hadley Trim whitespace
authored
51
da4b8cb @hadley New discretising functions to replace chop
authored
52 }
53
Something went wrong with that request. Please try again.