How to find the range of a vector in R?


The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function then diff function can be used to find the actual range. For example, if we have a vector x then the range can be found by using diff(range(x)).

Example

Live Demo

> x1<-rpois(10,2)
> x1

Output

[1] 4 2 3 0 2 3 1 3 4 2

Example

> diff(range(x1))

Output

[1] 4

Example

Live Demo

> x2<-rpois(150,5)
> x2

Output

[1]  4 5 3 10 2 4 2 4 8 7 3 1 5 6 7 3 7 3 4 5 3 7 2 7 5
[26] 6 5 5 5 4 3 5 9 6 9 9 2 9 7 6 6 4 4 3 9 4 8 4 5 5
[51] 9 7 2 8 6 6 2 4 6 2 9 3 2 8 3 5 4 4 4 8 3 6 1 2 4
[76] 6 8 4 3 7 5 3 5 6 3 4 7 6 5 7 9 10 5 8 5 2 9 2 6 4
[101] 5 8 2 2 2 5 3 5 6 6 4 8 6 11 3 4 7 1 8 8 4 1 10 5 6
[126] 5 9 8 4 5 2 4 6 7 4 5 8 5 4 5 3 2 6 5 4 4 2 7 5 3

Example

> diff(range(x2))

Output

[1] 10

Example

Live Demo

> x3<-rpois(150,30)
> x3

Output

[1]  25 33 25 27 16 36 28 34 38 26 37 42 24 27 26 24 30 45 30 27 27 37 29 28 26
[26] 32 24 25 33 38 41 37 29 24 35 28 36 23 22 22 31 38 24 26 26 27 45 24 30 36
[51] 29 27 16 27 30 28 35 43 34 40 29 24 21 25 32 24 33 30 32 34 34 29 19 30 25
[76] 24 31 25 26 32 26 29 32 29 28 30 27 29 25 29 28 28 31 26 28 28 20 24 34 44
[101] 25 26 39 28 23 32 33 28 31 39 28 24 28 20 36 31 38 32 27 29 43 38 37 27 28
[126] 25 32 25 36 34 35 24 33 24 22 24 21 32 23 29 38 23 25 20 31 43 27 29 35 25

Example

> diff(range(x3))

Output

[1] 29

Example

Live Demo

> x4<-sample(0:9,150,replace=TRUE)
> x4

Output

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

Example

> diff(range(x4))

Output

[1] 9

Example

Live Demo

> x5<-sample(501:600,120,replace=TRUE)
> x5

Output

[1] 540 565 570 516 554 567 537 529 506 538 505 598 541 509 585 576 557 544
[19] 545 584 555 552 569 552 559 510 588 505 522 589 534 588 583 507 574 529
[37] 587 593 578 592 536 584 575 545 572 569 542 513 531 558 548 503 565 550
[55] 554 569 512 592 548 590 555 560 583 512 531 530 597 544 578 597 527 598
[73] 543 573 530 597 542 576 532 583 591 585 578 592 600 553 573 511 506 574
[91] 600 583 526 553 584 569 561 523 550 503 584 531 584 590 548 597 514 512
[109] 546 518 591 563 542 559 509 557 579 590 563 538

Example

> diff(range(x5))

Output

[1] 97

Example

Live Demo

> x6<-sample(1000:9999,100)
> x6

Output

[1] 8830 1194 8928 6862 8048 9203 8094 8305 4013 8804 2148 1538 9715 9509 8167
[16] 2043 4125 3493 2995 9546 4397 7277 1828 4994 4912 2761 1443 6171 2311 7058
[31] 7210 7909 4190 1726 6829 8934 2767 8300 6578 6029 1821 2752 9243 7321 5489
[46] 8126 4740 8921 2596 7429 8272 2314 4505 6407 9296 8975 8995 3809 1865 4282
[61] 2636 7475 3037 5119 7013 4030 2871 5044 8971 7672 8409 1126 4515 6966 2925
[76] 4434 7380 4924 2922 1110 9281 7212 2992 7138 3398 1078 1024 7004 5909 2719
[91] 2427 3951 2274 8114 1211 5491 1856 5252 8955 4633

Example

> diff(range(x6))

Output

[1] 8691

Example

Live Demo

> x7<-rnorm(60,1,0.95)
> x7

Output

[1] 0.77732585 0.20340071 0.81968478 0.96607462 -1.20625378 1.13518917
[7] 1.49899894 0.54251191 0.72349942 1.31520105 -0.55316276 1.43105091
[13] 2.51926637 0.93088965 1.47562589 1.71550853 1.44065223 0.16561474
[19] -1.11521230 -0.75516868 1.51849795 1.45891592 2.09555983 -0.31360865
[25] 0.62348582 0.13172177 -0.05222773 0.83865212 0.93192698 1.29893029
[31] 2.96131722 2.02531440 2.00390166 1.17336233 2.22268897 -0.02296672
[37] 0.73884700 -0.65950707 1.55010346 1.56882173 1.87951054 0.72003358
[43] 0.68283502 0.58438396 -0.65058594 1.63307556 1.37260207 1.42002910
[49] 1.99269798 0.75585593 0.59646514 1.15462732 2.23040244 2.39621165
[55] -0.02079837 2.58771378 1.75922960 1.42400723 0.13761317 0.59743434

Example

> diff(range(x7))

Output

[1] 4.167571

Example

Live Demo

> x8<-rnorm(60,50,1.25)
> x8

Output

[1] 50.60171 47.35250 48.96147 49.62938 51.50854 50.61273 48.56096 50.97370
[9] 49.15675 48.92862 49.11240 50.99810 49.65265 50.65235 50.44551 49.28134
[17] 47.91846 50.89146 50.70412 50.04375 50.14076 48.45254 50.91452 50.35164
[25] 49.02045 49.70859 49.49023 50.81482 48.77840 48.94855 49.35256 49.94870
[33] 50.49340 50.16679 48.81921 49.94986 50.89710 50.78883 51.04020 51.74803
[41] 50.01834 50.79934 49.62719 49.23445 50.65635 49.59964 49.37007 50.52736
[49] 49.58866 51.01773 49.40808 49.47980 50.17246 48.21275 49.11339 51.04322
[57] 48.92094 50.60240 50.45600 49.84697

Example

> diff(range(x8))

Output

[1] 4.395527

Example

Live Demo

> x9<-runif(50,2,5)
> x9

Output

[1] 4.125960 2.396517 4.609997 2.473424 3.658801 2.407685 3.174801 4.383593
[9] 3.688843 2.125594 2.500938 4.126458 4.519009 4.411336 2.202806 2.290341
[17] 4.290165 4.287591 4.409094 3.121584 2.404941 4.626608 4.926589 4.700230
[25] 2.782749 2.844678 4.515919 3.670294 4.751767 4.942986 2.547970 3.278626
[33] 3.364280 4.102291 4.571044 3.554319 4.637315 4.143440 3.333218 3.997436
[41] 4.655782 3.663140 4.337505 4.517204 4.885042 3.040531 4.612726 2.489695
[49] 3.180844 3.445043

Example

> diff(range(x9))

Output

[1] 2.817393

Example

Live Demo

> x10<-rexp(50,3.12)
> x10

Output

[1] 0.361924533 0.363733351 0.188424290 0.227219438 0.131665583 0.142529389
[7] 0.557993442 0.431907105 0.532919218 0.281745467 0.140911293 0.079761168
[13] 0.303477710 0.289148983 0.615317513 0.005794841 0.435995172 0.818063367
[19] 0.412843319 1.323551486 0.911916495 0.422450781 0.476757946 0.214319709
[25] 0.081348798 0.142734230 0.016998688 0.201221571 0.067065224 0.075520322
[31] 0.850355486 0.044162291 0.090631643 0.045846146 0.448331579 0.049561155
[37] 0.231546764 0.342187336 0.223755116 0.258201262 0.293556476 0.689359642
[43] 0.120444026 0.649185021 0.146372525 0.010746677 0.094800106 0.602121528
[49] 0.028487216 0.007054710

Example

> diff(range(x10))

Output

[1] 1.317757

Updated on: 23-Nov-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements