binPack {BBmisc} | R Documentation |
Maps numeric items in x
into groups with sum
less or equal than capacity
.
A very simple greedy algorithm is used, which is not really optimized
for speed. This is a convenience function for smaller vectors, not
a competetive solver for the real binbacking problem.
If an element of x
exceeds capacity
, an error
is thrown.
binPack(x, capacity)
x |
[ |
capacity |
[ |
[integer
]. Integer with values “1” to “n.bins”
indicating bin membership.
x = 1:10
bp = binPack(x, 11)
xs = split(x, bp)
print(xs)
## $`1`
## [1] 1 10
##
## $`2`
## [1] 2 9
##
## $`3`
## [1] 3 8
##
## $`4`
## [1] 4 7
##
## $`5`
## [1] 5 6
print(sapply(xs, sum))
## 1 2 3 4 5
## 11 11 11 11 11