Different functions that perform binning.
cut_at(breaks)
nearest(centers)
bin_by_ntile(nbins)
bin_by_eqcut(nbins)
bin_by_pam(nbins)
bin_by_classInt(style, nbins = NULL)
A numeric vector of values that designate cut points between bins.
A numeric vector of values that designate the center of each bin.
The number of bins to split the data into.
a binning style (see classIntervals for details).
Each of these functions returns a function of a single numeric vector `x` that assigns each value of `x` to a bin.
x <- c(rnorm(10, 1, 1), rnorm(10, 3, 2), rnorm(20, 5, 3))
centers <- c(1, 3, 5)
nearest(centers)(x)
#> [1] 3 1 1 3 1 1 1 1 1 1 3 3 3 5 1 3 3 1 3 3 5 3 5 3 1 5 3 5 1 5 1 5 5 5 5 5 1 5
#> [39] 5 5
breaks <- c(2, 4)
cut_at(breaks)(x)
#> [1] "[2,4)" "[-3.52,2)" "[-3.52,2)" "[2,4)" "[-3.52,2)" "[-3.52,2)"
#> [7] "[-3.52,2)" "[-3.52,2)" "[-3.52,2)" "[-3.52,2)" "[2,4)" "[2,4)"
#> [13] "[2,4)" "[4,8.78]" "[-3.52,2)" "[2,4)" "[2,4)" "[-3.52,2)"
#> [19] "[2,4)" "[2,4)" "[4,8.78]" "[2,4)" "[4,8.78]" "[2,4)"
#> [25] "[-3.52,2)" "[4,8.78]" "[2,4)" "[4,8.78]" "[-3.52,2)" "[4,8.78]"
#> [31] "[-3.52,2)" "[4,8.78]" "[4,8.78]" "[4,8.78]" "[4,8.78]" "[4,8.78]"
#> [37] "[-3.52,2)" "[4,8.78]" "[4,8.78]" "[4,8.78]"
bin_by_eqcut(nbins=4)(x)
#> [1] "[1.33,3.2)" "[-3.52,1.33)" "[-3.52,1.33)" "[1.33,3.2)" "[-3.52,1.33)"
#> [6] "[1.33,3.2)" "[-3.52,1.33)" "[-3.52,1.33)" "[-3.52,1.33)" "[1.33,3.2)"
#> [11] "[3.2,6.16)" "[3.2,6.16)" "[1.33,3.2)" "[3.2,6.16)" "[-3.52,1.33)"
#> [16] "[1.33,3.2)" "[3.2,6.16)" "[1.33,3.2)" "[3.2,6.16)" "[3.2,6.16)"
#> [21] "[6.16,8.78]" "[1.33,3.2)" "[6.16,8.78]" "[1.33,3.2)" "[-3.52,1.33)"
#> [26] "[3.2,6.16)" "[3.2,6.16)" "[3.2,6.16)" "[-3.52,1.33)" "[6.16,8.78]"
#> [31] "[-3.52,1.33)" "[6.16,8.78]" "[6.16,8.78]" "[3.2,6.16)" "[6.16,8.78]"
#> [36] "[6.16,8.78]" "[1.33,3.2)" "[6.16,8.78]" "[6.16,8.78]" "[6.16,8.78]"
bin_by_ntile(nbins=4)(x)
#> [1] 2 1 1 2 1 2 1 1 1 2 3 3 2 3 1 2 3 2 3 3 4 2 4 2 1 3 3 3 1 4 1 4 4 3 4 4 2 4
#> [39] 4 4
bin_by_pam(nbins=4)(x)
#> [1] 3.211396 1.167246 1.167246 3.211396 1.167246 1.167246 1.167246
#> [8] 1.167246 1.167246 1.167246 3.211396 3.211396 1.167246 6.855176
#> [15] 1.167246 3.211396 3.211396 1.167246 3.211396 3.211396 6.855176
#> [22] 3.211396 6.855176 3.211396 -3.520710 6.855176 3.211396 3.211396
#> [29] 1.167246 6.855176 1.167246 6.855176 6.855176 6.855176 6.855176
#> [36] 6.855176 1.167246 6.855176 6.855176 6.855176
bin_by_classInt("pretty", nbins=4)(x)
#> [1] "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[-5,0)" "[-5,0)"
#> [9] "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[5,10]" "[0,5)" "[0,5)"
#> [17] "[0,5)" "[0,5)" "[0,5)" "[0,5)" "[5,10]" "[0,5)" "[5,10]" "[0,5)"
#> [25] "[-5,0)" "[5,10]" "[0,5)" "[0,5)" "[0,5)" "[5,10]" "[0,5)" "[5,10]"
#> [33] "[5,10]" "[5,10]" "[5,10]" "[5,10]" "[0,5)" "[5,10]" "[5,10]" "[5,10]"