The above solution has many imperfections, ie. when vector has many same values or
collection is odd, then findInterval method is not working as we wish.
Here is my simple solution
averageQuantile<- function(vec, value, value2) {
chunk = getChunkOfVector(vec, value, value2)
if(length(chunk) >0) {
return(mean(chunk))
}
return(0.0)
}
getChunkOfVector<- function(vector, value, value2) {
len = length(vector)
result<-vector()
vector<-sort(vector)
k<-1
for(i in vector){
if(k/len > value & k/len <= value2) {
result = append(result, i)
}
k<-k+1
}
return(result)
}
So if you simply need average of values between quantile(x, 0.25) and quantile(x, 0.5):
set.seed(1)
vec = rnorm(1000)
averageQuantile(vec, 0.25, 0.50)
# [1] -0.3397659