Skip to content
Prev 174982 / 398506 Next

Splitting Area under curve into equal portions

Nathan,

Someone will probably come up with a more elegant way, but does this help?
slice() will partition work into n groups where the sum in each group is
approximately the same.  slice() returns the index of the last element of
work[] for each group (except the last group).  The first group can be
indexed by 1:p[1]. The second by (p[1]+1):p[2] ... And the n-th group by
p[n-1]:N, where N <- length(work).

slice <- function(v, n){
  subtot <- floor(sum(v)/n)
  cumtot <- cumsum(v)
  p <- rep(0,n-1)
  for(i in 1:(n-1)) p[i] <- max(which(cumtot < (subtot*i)))
  p
  }

#to break work into ten groups
slice(work,10)


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA