rawAverage <- function(q = .5, M=1000, shrink = 10) { Batch <- numeric(M) K <- ceiling(M/shrink) Dist <- numeric(K) Weight <- 0. Position <- 1 medians <- seq(ceiling(shrink/2), by=shrink, len=K) merge <- function() { Batch <- sort(Batch) Wnew <- Weight + shrink Dist <- (Weight * Dist + shrink * Batch[medians])/Wnew Weight <- Wnew } list( function(dataItem) { Batch[[Position]] <- dataItem if(Position < M) Position <- Position+1 else { merge() Position <- 1 } }, function() quantile(Batch, q) ) }