bucketIndex <- function(v, N){
o <- rep(0, length(v))
curSum <- 0
index <- 1
for(i in seq(length(v))){
o[i] <- index
curSum <- curSum + v[i]
if(curSum > N){
curSum <- 0
index <- index + 1
}
}
o
}
> bucketIndex(c(1, 1, 2, 1, 5, 1), 3)
[1] 1 1 1 2 2 3
I’m wondering if this function is fundamentally un-vectorizable. If it is, is there some package to deal with this “class” of functions, or is the only alternative (if I want speed) to write it as a c extension?
I’m going to go out on a limb here and say the answer is “no.” Essentially, you’re changing what it is you sum over based on the results of the current sum. This means future calculations depend on the result of an intermediate calculation, which vectorized operations can’t do.