-4

I have columns of <customers> and <sales>. I want to calculate the <sales> values, corresponding to the relevant percentiles of <customers>. (e.g. sales volume corresponding to the upper 1% of customers, upper 10% of customers etc....

Hayk

G5W
  • 34,378
  • 10
  • 39
  • 71
Hayk
  • 27
  • 1
  • 7
  • Welcome to StackOverflow! Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and consider to add some sample data so we can help you out. – Lennyy May 18 '18 at 13:04

2 Answers2

1

I believe you are looking for the quantile() function. If you have a vector of values called a then you can compute the percentiles you want with: quantile(a, c(.01,.05,.5,.95)), if you want the first percentile, the first 5%, the mode and the 95%.

Yo B.
  • 613
  • 5
  • 15
  • Yes but I need to calculate the percent of sales, corresponding to the certain percentile of customers. – Hayk May 18 '18 at 14:08
0

Something like that for the upper 25%?

sum(df$Sale[df$Sale>=quantile(df$Sale,probs=.75)])/sum(df$Sale)
Nicolas2
  • 2,130
  • 1
  • 5
  • 14