How to find the range for 95% of all values in an R vector?

R ProgrammingServer Side ProgrammingProgramming

The range for 95% of all values actually represents the middle 95% values. Therefore, we can find the 2.5th percentile and 97.5th percentile so that the range for middle 95% can be obtained. For this purpose, we can use quantile function in R. To find the 2.5th percentile, we would need to use the probability = 0.025 and for the 97.5th percentile we can use probability = 0.0975.

Example

 Live Demo

x1<-1:10
x1

Output

[1] 1 2 3 4 5 6 7 8 9 10

Example

quantile(x1,probs=c(0.025,0.975))

Output

2.5% 97.5%
1.225 9.775

Example

 Live Demo

x2<-sample(0:9,200,replace=TRUE)
x2

Output

[1] 8 1 5 4 7 9 7 4 3 9 3 0 5 4 4 3 8 2 7 7 4 0 1 8 2 1 0 2 6 3 3 5 7 0 9 6 9
[38] 1 3 2 7 9 2 3 9 0 5 2 7 6 6 2 4 5 1 6 6 7 9 8 5 7 5 4 8 4 3 3 8 6 1 1 1 9
[75] 6 2 0 9 8 0 2 2 2 6 2 8 8 7 4 5 1 0 1 3 7 9 5 4 5 2 5 5 4 7 5 4 5 6 6 2 9
[112] 6 9 2 7 1 5 0 1 4 7 0 8 8 2 5 4 9 4 8 4 0 7 2 1 7 0 6 5 2 5 6 3 2 1 5 6 6
[149] 6 0 4 1 8 7 1 5 0 1 8 9 8 6 8 7 6 8 4 3 9 3 9 2 0 3 8 3 7 8 9 6 4 3 4 5 6
[186] 4 1 6 0 5 8 1 5 1 3 7 2 3 3 3

Example

> quantile(x2,probs=c(0.025,0.975))

Output

2.5% 97.5%
0 9

Example

 Live Demo

x3<-sample(1:100,200,replace=TRUE)
x3

Output

[1] 14 49 72 45 19 80 43 88 73 100 83 55 10 50 71 69 47 22
[19] 15 99 30 2 51 89 69 66 87 25 59 34 77 40 40 44 41 5
[37] 75 35 33 40 7 40 64 17 79 77 27 49 8 20 30 29 15 67
[55] 36 18 53 80 57 71 96 40 12 92 94 87 14 17 43 73 90 28
[73] 44 41 47 44 57 23 54 88 64 26 33 80 44 9 2 49 36 40
[91] 38 74 48 49 75 83 71 55 8 99 32 8 89 23 62 86 4 14
[109] 33 30 1 77 73 3 66 90 39 84 73 25 45 74 33 97 46 82
[127] 68 6 18 43 20 76 42 69 52 98 14 27 13 62 33 65 16 100
[145] 9 5 22 29 3 30 91 63 25 86 71 75 36 85 56 80 42 89
[163] 56 44 16 23 94 13 14 89 83 100 40 94 36 85 74 57 77 95
[181] 23 84 53 1 48 62 92 27 8 32 63 52 99 10 12 71 59 64
[199] 42 54

Example

quantile(x3,probs=c(0.025,0.975))

Output

2.5% 97.5%
3 99

Example

 Live Demo

x4<-sample(100:999,200)
x4

Output

[1] 820 234 148 100 865 811 694 864 197 140 815 588 158 521 542 115 675 932
[19] 169 494 257 549 963 340 595 814 324 182 952 291 936 601 743 794 610 377
[37] 495 179 284 739 484 901 627 376 609 220 784 418 721 488 738 944 712 458
[55] 551 166 138 857 801 785 500 722 989 394 517 640 238 688 485 426 637 133
[73] 881 504 625 809 445 916 200 802 306 955 581 937 466 639 247 502 146 740
[91] 413 655 767 996 886 935 723 361 286 131 269 167 186 959 396 805 665 218
[109] 696 883 253 454 400 949 175 777 758 971 691 395 603 435 934 406 843 281
[127] 553 976 267 888 127 913 178 987 787 354 290 977 225 539 995 803 419 288
[145] 221 294 550 442 525 782 366 586 501 875 567 543 828 451 830 821 992 342
[163] 737 611 806 753 876 487 449 313 719 578 983 160 768 556 933 680 956 375
[181] 761 678 279 398 755 139 330 686 824 321 819 335 580 674 348 671 246 509
[199] 447 499

Example

quantile(x4,probs=c(0.025,0.975))

Output

2.5% 97.5%
137.875 983.100

Example

 Live Demo

x5<-rnorm(80,5,2)
x5

Output

[1] 6.98972600 3.17248565 5.13234480 4.11901047 7.77081721 4.70660218
[7] 4.34543482 5.32969562 3.85105871 4.92334515 10.38088424 2.78494280
[13] 2.17723638 5.89778299 3.25433020 5.76595115 6.58821842 2.16789176
[19] 6.70022121 4.33298204 2.52347763 7.17797129 5.96444881 -0.49521879
[25] 3.32652489 6.36352461 7.02582094 4.76756040 0.05690139 3.60584007
[31] 6.43240996 1.91232232 4.03257916 9.08311081 8.88715843 7.76594592
[37] 5.80933391 3.37731011 3.14555718 6.14552974 5.65748181 2.88725211
[43] 6.81291913 5.28996898 6.84361973 4.71988891 3.13190129 3.45525499
[49] 6.02927444 4.99564376 7.46594963 0.70237604 5.29670524 4.31319790
[55] 4.31986763 2.19272850 4.03273654 6.97718448 4.50745487 4.36807171
[61] 7.39283829 2.14748082 4.48435806 4.51189441 5.35723362 8.93982200
[67] 6.83182644 4.27771018 7.26854753 3.78881429 4.19227762 4.24381458
[73] 2.93295093 7.40268495 3.35842472 3.73021960 5.85442187 7.90967270
[79] 2.90778517 6.01023427

Example

quantile(x5,probs=c(0.025,0.975))

Output

2.5% 97.5%
0.6862392 8.9434042

Example

 Live Demo

x6<-rnorm(50)
x6

Output

[1] 0.3331076074 0.6607651759 0.0240865785 -0.1448891552 -0.6969449129
[6] 0.0166860867 -1.2130240135 -0.9468299995 0.7802634147 0.9585927355
[11] -1.6272935094 1.3593188263 -0.0935888333 2.0885770523 -0.4199458516
[16] -0.0089055888 -0.1935638804 -1.6772774160 -2.0102456085 0.0009071378
[21] 1.9596100041 0.3502070761 0.6889992063 0.9485294768 0.4670174843
[26] 0.4977238683 -0.2571625431 -0.2327554066 -0.7488511452 0.7068129587
[31] 0.6054530742 1.6431915914 0.8637796961 -0.3100868562 1.9966920903
[36] 0.1412141929 0.3438465534 -0.3179982685 0.5358806944 1.9328734967
[41] -0.4732611071 -0.3964032732 -0.6116624673 -0.4275941636 0.3976625756
[46] 1.1928187412 0.5876758446 0.2999995481 0.3585979367 0.2490096352

Example

quantile(x6,probs=c(0.025,0.975))

Output

2.5% 97.5%
-1.666031 1.988349

Example

 Live Demo

x7<-rpois(120,5)
x7

Output

[1] 5 4 8 6 7 6 2 7 6 1 8 6 6 1 6 7 5 2 7 3 8 5 6 6 7
[26] 1 5 8 6 0 1 1 4 5 5 7 4 2 5 6 4 6 13 12 4 5 4 3 3 2
[51] 5 6 4 5 3 8 2 2 6 4 3 4 4 9 5 1 5 5 5 7 9 2 1 6 2
[76] 5 6 7 3 7 9 4 2 3 3 1 5 3 5 7 10 7 3 4 2 4 7 3 3 5
[101] 6 4 4 3 4 2 6 3 5 6 2 9 2 2 2 6 5 3 7 3

Example

quantile(x7,probs=c(0.025,0.975))

Output

2.5% 97.5%
1.000 9.025
raja
Published on 07-Dec-2020 06:21:09
Advertisements