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)

Arguments

breaks

A numeric vector of values that designate cut points between bins.

centers

A numeric vector of values that designate the center of each bin.

nbins

The number of bins to split the data into.

style

a binning style (see classIntervals for details).

Value

Each of these functions returns a function of a single numeric vector `x` that assigns each value of `x` to a bin.

Examples


x <- c(rnorm(10, 1, 1), rnorm(10, 3, 2), rnorm(20, 5, 3))
centers <- c(1, 3, 5)
nearest(centers)(x)
#>  [1] 1 1 1 1 1 1 3 1 1 1 1 3 5 1 3 3 1 5 5 3 3 5 3 1 5 5 5 5 5 5 1 5 5 5 5 3 5 5
#> [39] 5 5

breaks <- c(2, 4)
cut_at(breaks)(x)
#>  [1] "[-0.863,2)" "[-0.863,2)" "[-0.863,2)" "[-0.863,2)" "[-0.863,2)"
#>  [6] "[-0.863,2)" "[2,4)"      "[-0.863,2)" "[-0.863,2)" "[-0.863,2)"
#> [11] "[-0.863,2)" "[2,4)"      "[4,13.3]"   "[-0.863,2)" "[2,4)"     
#> [16] "[2,4)"      "[-0.863,2)" "[4,13.3]"   "[4,13.3]"   "[2,4)"     
#> [21] "[2,4)"      "[4,13.3]"   "[2,4)"      "[-0.863,2)" "[4,13.3]"  
#> [26] "[4,13.3]"   "[4,13.3]"   "[4,13.3]"   "[4,13.3]"   "[4,13.3]"  
#> [31] "[-0.863,2)" "[4,13.3]"   "[4,13.3]"   "[4,13.3]"   "[4,13.3]"  
#> [36] "[2,4)"      "[4,13.3]"   "[4,13.3]"   "[4,13.3]"   "[4,13.3]"  

bin_by_eqcut(nbins=4)(x)
#>  [1] "[-0.863,1.07)" "[-0.863,1.07)" "[-0.863,1.07)" "[-0.863,1.07)"
#>  [5] "[-0.863,1.07)" "[1.07,3.4)"    "[1.07,3.4)"    "[-0.863,1.07)"
#>  [9] "[1.07,3.4)"    "[-0.863,1.07)" "[1.07,3.4)"    "[1.07,3.4)"   
#> [13] "[3.4,5.02)"    "[1.07,3.4)"    "[3.4,5.02)"    "[3.4,5.02)"   
#> [17] "[-0.863,1.07)" "[3.4,5.02)"    "[5.02,13.3]"   "[1.07,3.4)"   
#> [21] "[1.07,3.4)"    "[3.4,5.02)"    "[1.07,3.4)"    "[-0.863,1.07)"
#> [25] "[5.02,13.3]"   "[5.02,13.3]"   "[5.02,13.3]"   "[5.02,13.3]"  
#> [29] "[5.02,13.3]"   "[3.4,5.02)"    "[-0.863,1.07)" "[3.4,5.02)"   
#> [33] "[3.4,5.02)"    "[5.02,13.3]"   "[5.02,13.3]"   "[1.07,3.4)"   
#> [37] "[3.4,5.02)"    "[3.4,5.02)"    "[5.02,13.3]"   "[5.02,13.3]"  
bin_by_ntile(nbins=4)(x)
#>  [1] 1 1 1 1 1 2 2 1 2 1 2 2 3 2 3 3 1 3 4 2 2 3 2 1 4 4 4 4 4 3 1 3 3 4 4 2 3 3
#> [39] 4 4

bin_by_pam(nbins=4)(x)
#>  [1] 0.462801 0.462801 0.462801 0.462801 0.462801 0.462801 2.894796 0.462801
#>  [9] 0.462801 0.462801 2.894796 2.894796 4.850105 0.462801 4.850105 2.894796
#> [17] 0.462801 4.850105 8.201924 2.894796 2.894796 4.850105 2.894796 0.462801
#> [25] 8.201924 4.850105 4.850105 8.201924 4.850105 4.850105 0.462801 4.850105
#> [33] 4.850105 8.201924 4.850105 2.894796 4.850105 4.850105 4.850105 8.201924
bin_by_classInt("pretty", nbins=4)(x)
#>  [1] "[-5,0)"  "[0,5)"   "[0,5)"   "[0,5)"   "[0,5)"   "[0,5)"   "[0,5)"  
#>  [8] "[-5,0)"  "[0,5)"   "[-5,0)"  "[0,5)"   "[0,5)"   "[0,5)"   "[0,5)"  
#> [15] "[0,5)"   "[0,5)"   "[0,5)"   "[0,5)"   "[5,10)"  "[0,5)"   "[0,5)"  
#> [22] "[0,5)"   "[0,5)"   "[0,5)"   "[5,10)"  "[5,10)"  "[5,10)"  "[5,10)" 
#> [29] "[5,10)"  "[0,5)"   "[-5,0)"  "[0,5)"   "[0,5)"   "[5,10)"  "[5,10)" 
#> [36] "[0,5)"   "[0,5)"   "[0,5)"   "[5,10)"  "[10,15]"