bin.windows creates binned ranges based on the minimum factor of the integer input (i) or user-supplied value.

bin.windows(
  i = 1,
  use.bin = NULL,
  as.factor = FALSE,
  label_format = "<%s,%s>",
  silently = FALSE,
  ...
)

Arguments

i

(integer[]) An integer scalar, vector, or n-dimensional object executed conditionally as follows:

  • if a vector of length = 1, a zero-based sequence up to abs(i) is used

  • if a vector of length = 2, a sequence is created from the values in the order given if the input is non-dimensional

  • if a vector of length >= 3 or i is dimensional, the raw values coerced into a vector

use.bin

The bin size to use: when empty, the smallest prime number or integer factor in i within the range of i is used.

as.factor

(logical) Should the output be converted into a factor?

label_format

(string) A two-argument string compatible with sprintf that controls label output

silently

(logical) Should the output return invisibly?

...

(not used)

Value

A character (or factor) vector the length of the input, as "binned" representations. If the input is dimensional, an array of the same dimensions is returned

Note

Factor output is only available for heterogeneous data structures.

See also

Other Data Generation: make.date_time(), make.windows()

Examples

bin.windows(40, use.bin = 3)
#>       [,1]     
#>  [1,] "<0,2>"  
#>  [2,] "<0,2>"  
#>  [3,] "<0,2>"  
#>  [4,] "<3,5>"  
#>  [5,] "<3,5>"  
#>  [6,] "<3,5>"  
#>  [7,] "<6,8>"  
#>  [8,] "<6,8>"  
#>  [9,] "<6,8>"  
#> [10,] "<9,11>" 
#> [11,] "<9,11>" 
#> [12,] "<9,11>" 
#> [13,] "<12,14>"
#> [14,] "<12,14>"
#> [15,] "<12,14>"
#> [16,] "<15,17>"
#> [17,] "<15,17>"
#> [18,] "<15,17>"
#> [19,] "<18,20>"
#> [20,] "<18,20>"
#> [21,] "<18,20>"
#> [22,] "<21,23>"
#> [23,] "<21,23>"
#> [24,] "<21,23>"
#> [25,] "<24,26>"
#> [26,] "<24,26>"
#> [27,] "<24,26>"
#> [28,] "<27,29>"
#> [29,] "<27,29>"
#> [30,] "<27,29>"
#> [31,] "<30,32>"
#> [32,] "<30,32>"
#> [33,] "<30,32>"
#> [34,] "<33,35>"
#> [35,] "<33,35>"
#> [36,] "<33,35>"
#> [37,] "<36,38>"
#> [38,] "<36,38>"
#> [39,] "<36,38>"
#> [40,] "<39,41>"
#> [41,] "<39,41>"
#> attr(,"bin.map")
#>       label win.vals
#>      <char>   <list>
#>  1:   <0,2>      0,2
#>  2:   <3,5>      3,5
#>  3:   <6,8>      6,8
#>  4:  <9,11>     9,11
#>  5: <12,14>    12,14
#>  6: <15,17>    15,17
#>  7: <18,20>    18,20
#>  8: <21,23>    21,23
#>  9: <24,26>    24,26
#> 10: <27,29>    27,29
#> 11: <30,32>    30,32
#> 12: <33,35>    33,35
#> 13: <36,38>    36,38
#> 14: <39,41>    39,40
#> attr(,"bin.size")
#> [1] 3
bin.windows(40, use.bin = 3, as.factor = TRUE)
#>       [,1]   
#>  [1,] <0,2>  
#>  [2,] <0,2>  
#>  [3,] <0,2>  
#>  [4,] <3,5>  
#>  [5,] <3,5>  
#>  [6,] <3,5>  
#>  [7,] <6,8>  
#>  [8,] <6,8>  
#>  [9,] <6,8>  
#> [10,] <9,11> 
#> [11,] <9,11> 
#> [12,] <9,11> 
#> [13,] <12,14>
#> [14,] <12,14>
#> [15,] <12,14>
#> [16,] <15,17>
#> [17,] <15,17>
#> [18,] <15,17>
#> [19,] <18,20>
#> [20,] <18,20>
#> [21,] <18,20>
#> [22,] <21,23>
#> [23,] <21,23>
#> [24,] <21,23>
#> [25,] <24,26>
#> [26,] <24,26>
#> [27,] <24,26>
#> [28,] <27,29>
#> [29,] <27,29>
#> [30,] <27,29>
#> [31,] <30,32>
#> [32,] <30,32>
#> [33,] <30,32>
#> [34,] <33,35>
#> [35,] <33,35>
#> [36,] <33,35>
#> [37,] <36,38>
#> [38,] <36,38>
#> [39,] <36,38>
#> [40,] <39,41>
#> [41,] <39,41>
#> 14 Levels: <0,2> < <3,5> < <6,8> < <9,11> < <12,14> < <15,17> < ... < <39,41>
bin.windows(40, use.bin = 3, silently = TRUE)

.Data <- c(5, 50)
list(before = .Data, after = bin.windows(.Data, use.bin = 3))
#> $before
#> [1]  5 50
#> 
#> $after
#>       [,1]     
#>  [1,] "<3,5>"  
#>  [2,] "<6,8>"  
#>  [3,] "<6,8>"  
#>  [4,] "<6,8>"  
#>  [5,] "<9,11>" 
#>  [6,] "<9,11>" 
#>  [7,] "<9,11>" 
#>  [8,] "<12,14>"
#>  [9,] "<12,14>"
#> [10,] "<12,14>"
#> [11,] "<15,17>"
#> [12,] "<15,17>"
#> [13,] "<15,17>"
#> [14,] "<18,20>"
#> [15,] "<18,20>"
#> [16,] "<18,20>"
#> [17,] "<21,23>"
#> [18,] "<21,23>"
#> [19,] "<21,23>"
#> [20,] "<24,26>"
#> [21,] "<24,26>"
#> [22,] "<24,26>"
#> [23,] "<27,29>"
#> [24,] "<27,29>"
#> [25,] "<27,29>"
#> [26,] "<30,32>"
#> [27,] "<30,32>"
#> [28,] "<30,32>"
#> [29,] "<33,35>"
#> [30,] "<33,35>"
#> [31,] "<33,35>"
#> [32,] "<36,38>"
#> [33,] "<36,38>"
#> [34,] "<36,38>"
#> [35,] "<39,41>"
#> [36,] "<39,41>"
#> [37,] "<39,41>"
#> [38,] "<42,44>"
#> [39,] "<42,44>"
#> [40,] "<42,44>"
#> [41,] "<45,47>"
#> [42,] "<45,47>"
#> [43,] "<45,47>"
#> [44,] "<48,50>"
#> [45,] "<48,50>"
#> [46,] "<48,50>"
#> attr(,"bin.map")
#>       label win.vals
#>      <char>   <list>
#>  1:   <3,5>      5,5
#>  2:   <6,8>      6,8
#>  3:  <9,11>     9,11
#>  4: <12,14>    12,14
#>  5: <15,17>    15,17
#>  6: <18,20>    18,20
#>  7: <21,23>    21,23
#>  8: <24,26>    24,26
#>  9: <27,29>    27,29
#> 10: <30,32>    30,32
#> 11: <33,35>    33,35
#> 12: <36,38>    36,38
#> 13: <39,41>    39,41
#> 14: <42,44>    42,44
#> 15: <45,47>    45,47
#> 16: <48,50>    48,50
#> attr(,"bin.size")
#> [1] 3
#> 

.Data <- array(1:10, dim = c(5, 2))
list(before = .Data, after = bin.windows(.Data, use.bin = 3, as.factor = FALSE))
#> $before
#>      [,1] [,2]
#> [1,]    1    6
#> [2,]    2    7
#> [3,]    3    8
#> [4,]    4    9
#> [5,]    5   10
#> 
#> $after
#>      [,1]    [,2]    
#> [1,] "<0,2>" "<6,8>" 
#> [2,] "<0,2>" "<6,8>" 
#> [3,] "<3,5>" "<6,8>" 
#> [4,] "<3,5>" "<9,11>"
#> [5,] "<3,5>" "<9,11>"
#> attr(,"bin.map")
#>     label win.vals
#>    <char>   <list>
#> 1:  <0,2>      1,2
#> 2:  <3,5>      3,5
#> 3:  <6,8>      6,8
#> 4: <9,11>     9,10
#> attr(,"bin.size")
#> [1] 3
#> 
list(before = .Data, after = bin.windows(.Data, use.bin = 3, as.factor = TRUE))
#> $before
#>      [,1] [,2]
#> [1,]    1    6
#> [2,]    2    7
#> [3,]    3    8
#> [4,]    4    9
#> [5,]    5   10
#> 
#> $after
#>      [,1]  [,2]  
#> [1,] <0,2> <6,8> 
#> [2,] <0,2> <6,8> 
#> [3,] <3,5> <6,8> 
#> [4,] <3,5> <9,11>
#> [5,] <3,5> <9,11>
#> Levels: <0,2> < <3,5> < <6,8> < <9,11>
#> 

.Data <- cbind(
    a = sample(70, 30)
    , b = sample(100, 30)
    , c = sample(10, 30, TRUE)
    )

list(before = .Data, after = bin.windows(.Data, use.bin = 7))
#> $before
#>        a  b  c
#>  [1,] 58 95  7
#>  [2,] 54 32  8
#>  [3,] 70 55  7
#>  [4,] 23 79  9
#>  [5,]  8 27  7
#>  [6,] 11 71  9
#>  [7,] 66 91  2
#>  [8,] 15 78  2
#>  [9,] 44 11  6
#> [10,] 62 24  8
#> [11,] 19 20  9
#> [12,] 17 49  5
#> [13,] 52  2  2
#> [14,] 57 43  2
#> [15,] 48 94  4
#> [16,] 43 83  5
#> [17,] 37 40  7
#> [18,] 32 61  4
#> [19,] 64 25  4
#> [20,] 14 54  7
#> [21,]  5 52  1
#> [22,] 13  6 10
#> [23,] 33 70  1
#> [24,] 65 45  1
#> [25,] 68 80  1
#> [26,] 47 57  2
#> [27,]  9 93  1
#> [28,] 45 44  3
#> [29,] 51 34  7
#> [30,] 30 17  9
#> 
#> $after
#>       a         b         c       
#>  [1,] "<56,62>" "<91,97>" "<7,13>"
#>  [2,] "<49,55>" "<28,34>" "<7,13>"
#>  [3,] "<70,76>" "<49,55>" "<7,13>"
#>  [4,] "<21,27>" "<77,83>" "<7,13>"
#>  [5,] "<7,13>"  "<21,27>" "<7,13>"
#>  [6,] "<7,13>"  "<70,76>" "<7,13>"
#>  [7,] "<63,69>" "<91,97>" "<0,6>" 
#>  [8,] "<14,20>" "<77,83>" "<0,6>" 
#>  [9,] "<42,48>" "<7,13>"  "<0,6>" 
#> [10,] "<56,62>" "<21,27>" "<7,13>"
#> [11,] "<14,20>" "<14,20>" "<7,13>"
#> [12,] "<14,20>" "<49,55>" "<0,6>" 
#> [13,] "<49,55>" "<0,6>"   "<0,6>" 
#> [14,] "<56,62>" "<42,48>" "<0,6>" 
#> [15,] "<42,48>" "<91,97>" "<0,6>" 
#> [16,] "<42,48>" "<77,83>" "<0,6>" 
#> [17,] "<35,41>" "<35,41>" "<7,13>"
#> [18,] "<28,34>" "<56,62>" "<0,6>" 
#> [19,] "<63,69>" "<21,27>" "<0,6>" 
#> [20,] "<14,20>" "<49,55>" "<7,13>"
#> [21,] "<0,6>"   "<49,55>" "<0,6>" 
#> [22,] "<7,13>"  "<0,6>"   "<7,13>"
#> [23,] "<28,34>" "<70,76>" "<0,6>" 
#> [24,] "<63,69>" "<42,48>" "<0,6>" 
#> [25,] "<63,69>" "<77,83>" "<0,6>" 
#> [26,] "<42,48>" "<56,62>" "<0,6>" 
#> [27,] "<7,13>"  "<91,97>" "<0,6>" 
#> [28,] "<42,48>" "<42,48>" "<0,6>" 
#> [29,] "<49,55>" "<28,34>" "<7,13>"
#> [30,] "<28,34>" "<14,20>" "<7,13>"
#> attr(,"bin.map")
#>       label win.vals
#>      <char>   <list>
#>  1:   <0,6>      1,6
#>  2:  <7,13>     7,13
#>  3: <14,20>    14,20
#>  4: <21,27>    23,27
#>  5: <28,34>    30,34
#>  6: <35,41>    37,40
#>  7: <42,48>    43,48
#>  8: <49,55>    49,55
#>  9: <56,62>    57,62
#> 10: <63,69>    64,68
#> 11: <70,76>    70,71
#> 12: <77,83>    78,83
#> 13: <91,97>    91,95
#> attr(,"bin.size")
#> [1] 7
#> 
list(before = .Data, after = as.data.frame(.Data) |> bin.windows(use.bin = 7))
#> $before
#>        a  b  c
#>  [1,] 58 95  7
#>  [2,] 54 32  8
#>  [3,] 70 55  7
#>  [4,] 23 79  9
#>  [5,]  8 27  7
#>  [6,] 11 71  9
#>  [7,] 66 91  2
#>  [8,] 15 78  2
#>  [9,] 44 11  6
#> [10,] 62 24  8
#> [11,] 19 20  9
#> [12,] 17 49  5
#> [13,] 52  2  2
#> [14,] 57 43  2
#> [15,] 48 94  4
#> [16,] 43 83  5
#> [17,] 37 40  7
#> [18,] 32 61  4
#> [19,] 64 25  4
#> [20,] 14 54  7
#> [21,]  5 52  1
#> [22,] 13  6 10
#> [23,] 33 70  1
#> [24,] 65 45  1
#> [25,] 68 80  1
#> [26,] 47 57  2
#> [27,]  9 93  1
#> [28,] 45 44  3
#> [29,] 51 34  7
#> [30,] 30 17  9
#> 
#> $after
#>    a         b         c       
#> 1  "<56,62>" "<91,97>" "<7,13>"
#> 2  "<49,55>" "<28,34>" "<7,13>"
#> 3  "<70,76>" "<49,55>" "<7,13>"
#> 4  "<21,27>" "<77,83>" "<7,13>"
#> 5  "<7,13>"  "<21,27>" "<7,13>"
#> 6  "<7,13>"  "<70,76>" "<7,13>"
#> 7  "<63,69>" "<91,97>" "<0,6>" 
#> 8  "<14,20>" "<77,83>" "<0,6>" 
#> 9  "<42,48>" "<7,13>"  "<0,6>" 
#> 10 "<56,62>" "<21,27>" "<7,13>"
#> 11 "<14,20>" "<14,20>" "<7,13>"
#> 12 "<14,20>" "<49,55>" "<0,6>" 
#> 13 "<49,55>" "<0,6>"   "<0,6>" 
#> 14 "<56,62>" "<42,48>" "<0,6>" 
#> 15 "<42,48>" "<91,97>" "<0,6>" 
#> 16 "<42,48>" "<77,83>" "<0,6>" 
#> 17 "<35,41>" "<35,41>" "<7,13>"
#> 18 "<28,34>" "<56,62>" "<0,6>" 
#> 19 "<63,69>" "<21,27>" "<0,6>" 
#> 20 "<14,20>" "<49,55>" "<7,13>"
#> 21 "<0,6>"   "<49,55>" "<0,6>" 
#> 22 "<7,13>"  "<0,6>"   "<7,13>"
#> 23 "<28,34>" "<70,76>" "<0,6>" 
#> 24 "<63,69>" "<42,48>" "<0,6>" 
#> 25 "<63,69>" "<77,83>" "<0,6>" 
#> 26 "<42,48>" "<56,62>" "<0,6>" 
#> 27 "<7,13>"  "<91,97>" "<0,6>" 
#> 28 "<42,48>" "<42,48>" "<0,6>" 
#> 29 "<49,55>" "<28,34>" "<7,13>"
#> 30 "<28,34>" "<14,20>" "<7,13>"
#> attr(,"bin.map")
#>       label win.vals
#>      <char>   <list>
#>  1:   <0,6>      1,6
#>  2:  <7,13>     7,13
#>  3: <14,20>    14,20
#>  4: <21,27>    23,27
#>  5: <28,34>    30,34
#>  6: <35,41>    37,40
#>  7: <42,48>    43,48
#>  8: <49,55>    49,55
#>  9: <56,62>    57,62
#> 10: <63,69>    64,68
#> 11: <70,76>    70,71
#> 12: <77,83>    78,83
#> 13: <91,97>    91,95
#> attr(,"bin.size")
#> [1] 7
#>