How to check whether the elements of a vector are arranged in an increasing order or decreasing order?


A vector can contain values that are increasing or decreasing in nature or they can be also random which means a higher value may come after a lower one which is followed by a higher value. An example of increasing arrangement of elements of vector is 1, 2, 3 and the opposite of that would be decreasing arrangement. We can check whether a vector is arranged in increasing order or decreasing order by checking whether the difference between all values of the vector is greater than or equal to zero or not and it can be done by using diff function.

Examples

 Live Demo

> x1<-1:10
> x1

Output

[1] 1 2 3 4 5 6 7 8 9 10
> all(diff(x1)>=0)

Output

[1] TRUE

Example

 Live Demo

> x2<-sample(1:10,50,replace=TRUE)
> x2

Output

[1] 7 8 8 4 8 6 10 9 2 10 2 4 5 4 6 10 2 3 8 2 4 10 3 7 5
[26] 2 7 2 3 6 1 6 3 8 8 8 2 7 9 1 7 1 5 9 6 10 10 8 3 3
> all(diff(x2)>=0)

Output

[1] FALSE

Example

 Live Demo

> x3<-10:1
> x3

Output

[1] 10 9 8 7 6 5 4 3 2 1
> all(diff(x3)>=0)

Output

[1] FALSE
> all(diff(x3)<=0)

Output

[1] TRUE

Example

 Live Demo

> x4<-c(14,17,14,15,29,24,17,18,38)
> x4

Output

[1] 14 17 14 15 29 24 17 18 38


> all(diff(x4)>=0)

Output

[1] FALSE
> all(diff(x4)<=0)

Output

[1] FALSE

Example

 Live Demo

> x5<-sample(1:50,100,replace=TRUE)
> x5

Output

[1] 23 43 7 16 7 41 21 48 3 28 8 45 37 12 39 43 20 50 33 16 17 35 44 9 29
[26] 27 44 46 15 29 15 17 4 17 8 36 50 32 50 17 33 34 30 31 40 16 22 32 39 21
[51] 40 39 16 31 38 28 3 22 42 31 48 39 15 14 45 15 3 20 50 23 33 39 10 20 41
[76] 49 20 1 40 19 7 1 7 12 16 23 50 3 44 27 40 12 31 12 47 44 18 29 34 32
> all(diff(x5)>=0)

Output

[1] FALSE
> all(diff(x5)<=0)

Output

[1] FALSE

Example

 Live Demo

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

Output

[1] 418 260 637 80 646 396 506 791 226 966 468 866 659 415 110 520 128 236
[19] 316 357 255 162 645 867 569 838 997 819 501 913 562 601 24 902 943 758
[37] 716 51 2 215 958 946 249 133 853 67 560 574 917 179 983 517 808 595
[55] 36 332 145 31 504 824 774 663 325 802 892 777 593 35 957 196 805 690
[73] 152 7 441 481 871 786 585 693 563 695 383 273 375 143 794 865 901 915
[91] 581 638 203 94 510 723 346 459 180 746
> all(diff(x6)>=0)

Output

[1] FALSE
> all(diff(x6)<=0)

Output

[1] FALSE

Example

 Live Demo

> x7<-rnorm(50,2)
> x7

Output

[1] 0.84261189 2.34635094 3.26053336 1.95924074 3.04214951 1.44615498
[7] 1.76529970 1.83573367 3.78949542 -0.50112590 1.05013127 1.69607013
[13] 1.45119098 2.33131602 1.01201612 2.28410496 0.64955391 2.59566288
[19] 2.08781663 2.22674640 -0.05599111 1.57843878 2.96839553 2.84797728
[25] 1.02880709 2.74873674 2.17837761 1.08775763 3.38554948 3.44496879
[31] 1.75175242 2.68978154 2.87401839 2.11086756 2.14189161 4.09328392
[37] 0.17228319 3.80599050 2.79396647 4.11371253 1.70642279 3.14290420
[43] 2.26725813 2.44773929 2.39364352 0.62493820 1.57672865 0.90599779
[49] 3.72155473 2.11842467
> all(diff(x7)>=0)

Output

[1] FALSE

Example

 Live Demo

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

Output

[1] 3 2 7 3 7 5 3 9 9 7 3 6 6 3 4 8 5 8 3 4 9 5 10 8 6
[26] 8 8 4 3 2 3 6 6 4 4 6 4 6 7 5 5 2 3 9 9 7 5 3 5 8
[51] 6 7 4 3 6 4 4 4 6 5 5 10 6 8 1 2 5 8 5 5 5 5 3 5 3
[76] 7 7 8 5 9 2 4 5 5 5 2 6 4 6 7 8 5 8 5 2 3 6 7 7 1
> all(diff(x8)<=0)

Output

[1] FALSE
> all(diff(x8)>=0)

Output

[1] FALSE

Updated on: 04-Sep-2020

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements