How to determine the percentiles of a vector values in R?


Percentile helps us to determine the values that lie at a certain percent in a data set. For example, if we have a vector of size 100 with containing any values and suppose that the tenth-percentile of the vector is 25, which means there are ten percent values in the vector that are less than 25, or we can say, there are ninety percent values in the vector that are greater than 25. We can find percentiles of a vector values using quantile function in R.

Examples

 Live Demo

> x1<-rpois(100,5)
> x1

Output

[1] 7 1 7 6 6 5 3 1 5 5 4 5 6 2 4 7 7 3 4 7 7 4 3 6 2
[26] 1 14 1 4 8 6 4 6 7 4 5 6 6 6 10 4 2 5 3 5 4 10 4 3 6
[51] 3 9 5 3 6 10 6 4 4 7 3 3 8 4 4 7 3 1 4 5 4 4 7 5 5
[76] 9 7 3 4 10 5 6 4 7 5 8 3 4 2 3 10 4 6 7 2 3 4 2 2 11
> quantile(x1,probs=seq(0,1,by=0.01))

Output

0% 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% 11% 12%
1.00 1.00 1.00 1.00 1.00 1.95 2.00 2.00 2.00 2.00 2.00 2.00 2.88
13% 14% 15% 16% 17% 18% 19% 20% 21% 22% 23% 24% 25%
3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00 3.00
26% 27% 28% 29% 30% 31% 32% 33% 34% 35% 36% 37% 38%
3.74 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00
39% 40% 41% 42% 43% 44% 45% 46% 47% 48% 49% 50% 51%
4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.52 5.00 5.00 5.00
52% 53% 54% 55% 56% 57% 58% 59% 60% 61% 62% 63% 64%
5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.00 5.39 6.00 6.00 6.00
65% 66% 67% 68% 69% 70% 71% 72% 73% 74% 75% 76% 77%
6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 6.25 7.00 7.00
78% 79% 80% 81% 82% 83% 84% 85% 86% 87% 88% 89% 90%
7.00 7.00 7.00 7.00 7.00 7.00 7.00 7.00 7.00 7.00 7.12 8.00 8.00
91% 92% 93% 94% 95% 96% 97% 98% 99% 100%
8.09 9.00 9.07 10.00 10.00 10.00 10.00 10.02 11.03 14.00
> quantile(x1,probs=seq(0,1,by=0.02))

Output

0% 2% 4% 6% 8% 10% 12% 14% 16% 18% 20% 22% 24%
1.00 1.00 1.00 2.00 2.00 2.00 2.88 3.00 3.00 3.00 3.00 3.00 3.00
26% 28% 30% 32% 34% 36% 38% 40% 42% 44% 46% 48% 50%
3.74 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.00 4.52 5.00
52% 54% 56% 58% 60% 62% 64% 66% 68% 70% 72% 74% 76%
5.00 5.00 5.00 5.00 5.00 6.00 6.00 6.00 6.00 6.00 6.00 6.00 7.00
78% 80% 82% 84% 86% 88% 90% 92% 94% 96% 98% 100%
7.00 7.00 7.00 7.00 7.00 7.12 8.00 9.00 10.00 10.00 10.02 14.00
> quantile(x1,probs=seq(0,1,by=0.05))

Output

0% 5% 10% 15% 20% 25% 30% 35% 40% 45% 50% 55% 60%
1.00 1.95 2.00 3.00 3.00 3.00 4.00 4.00 4.00 4.00 5.00 5.00 5.00
65% 70% 75% 80% 85% 90% 95% 100%
6.00 6.00 6.25 7.00 7.00 8.00 10.00 14.00
> quantile(x1,probs=seq(0,1,by=0.10))

Output

0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
1 2 3 4 4 5 5 6 7 8 14
> quantile(x1,probs=seq(0,1,by=0.25))

Output

0% 25% 50% 75% 100%
1.00 3.00 5.00 6.25 14.00

Example

 Live Demo

> x2<-sample(1:1000,100)
> x2

Output

[1] 315 259 494 865 760 289 48 331 100 108 301 10 170 280 348 402 209 468
[19] 827 649 309 395 991 8 626 261 541 306 326 74 282 585 267 887 262 736
[37] 204 723 219 696 352 667 119 452 856 924 579 622 936 646 36 55 490 240
[55] 891 632 862 304 989 665 422 612 105 793 388 463 180 278 373 241 24 679
[73] 559 703 37 686 566 303 719 912 19 712 671 378 549 615 244 994 188 464
[91] 393 139 299 371 670 189 311 905 418 569
> quantile(x2,probs=seq(0,1,by=0.01))

Output

0% 1% 2% 3% 4% 5% 6% 7% 8% 9% 10%
8.00 9.98 18.82 23.85 35.52 36.95 47.34 54.51 72.48 97.66 104.50
11% 12% 13% 14% 15% 16% 17% 18% 19% 20% 21%
107.67 117.68 136.40 165.66 178.50 186.72 188.83 201.30 208.05 217.00 235.59
22% 23% 24% 25% 26% 27% 28% 29% 30% 31% 32%
240.78 243.31 255.40 260.50 261.74 265.65 274.92 279.42 281.40 286.83 295.80
33% 34% 35% 36% 37% 38% 39% 40% 41% 42% 43%
300.34 302.32 303.65 305.28 307.89 310.24 313.44 321.60 328.95 340.86 350.28
44% 45% 46% 47% 48% 49% 50% 51% 52% 53% 54%
362.64 372.10 375.70 383.30 390.60 394.02 398.50 409.84 419.92 436.10 457.06
55% 56% 57% 58% 59% 60% 61% 62% 63% 64% 65%
463.45 465.76 477.46 491.68 513.27 544.20 552.90 561.66 567.11 572.60 581.10
66% 67% 68% 69% 70% 71% 72% 73% 74% 75% 76%
594.18 612.99 617.24 623.24 627.80 636.06 646.84 653.32 665.52 667.75 670.24
77% 78% 79% 80% 81% 82% 83% 84% 85% 86% 87%
672.84 680.54 688.10 697.40 704.71 713.26 719.68 725.08 739.60 764.62 797.42
88% 89% 90% 91% 92% 93% 94% 95% 96% 97% 98%
830.48 856.66 862.30 866.98 887.32 891.98 905.42 912.60 924.48 937.59 989.04
99% 100%
991.03 994.00
> quantile(x2,probs=seq(0,1,by=0.05))

Output

0% 5% 10% 15% 20% 25% 30% 35% 40% 45% 50%
8.00 36.95 104.50 178.50 217.00 260.50 281.40 303.65 321.60 372.10 398.50
55% 60% 65% 70% 75% 80% 85% 90% 95% 100%
463.45 544.20 581.10 627.80 667.75 697.40 739.60 862.30 912.60 994.00
> quantile(x2,probs=seq(0,1,by=0.25))

Output

0% 25% 50% 75% 100%
8.00 260.50 398.50 667.75 994.00

Updated on: 04-Sep-2020

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements