idomeneus
idomeneus

Reputation: 153

Sum of values by quantile

I have a simple question. Is there a function that generates the sum of values for a set of predetermined quantiles?

Say I have a vector example.data with 100 values

I run quantile(example.data, seq(0,1, by = 0.1))and I get

0%   10%   20%   30%   40%   50%   60%   70%   80%   90% 
0.040 0.090 0.186 0.227 0.336 0.450 0.584 0.670 0.730 0.832 
 100% 
1.000 

For each quantile I would like to see not the cut-off point, but the sum of the values for the cases in each quantile. Is there a parameter in quantile, or a similar function that includes such parameter?

Thanks much...

Upvotes: 2

Views: 2779

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

quantile gives you quantiles, not sums of values inside quantiles: those are fundamentally different things.

However, you can use cut and split to get the result you want:

q = quantile(x, seq(0, 1, by = 0.1))
cuts = cut(x, q)
values_per_quantile = split(x, cuts)
sums = sapply(values_per_quantile, sum)

Upvotes: 2

nicola
nicola

Reputation: 24480

Try:

tapply(example.data,findInterval(example.data,quantile(example.data, seq(0,1, by = 0.1))),sum)

Upvotes: 1

Related Questions