How to check if values in a column of an R data frame are increasingly ordered or not?



The values are increasingly ordered if the first value is less than the second, the second is less than the third, the third is less than the fourth, the fourth is less than the fifth, and so on. In base R, we have a function called is.unsorted that can help us to determine whether the values in a column of an R data frame are increasingly ordered or not. Check out the below examples to understand how it works.

Example1

Live Demo

> set.seed(3257) > x<-rpois(20,8) > df1<-data.frame(x) > df1

Output

 x 1 9 2 8 3 8 4 7 5 10 6 2 7 7 8 7 9 7 10 9 11 10 12 6 13 9 14 9 15 9 16 11 17 12 18 8 19 10 20 12

Example

> is.unsorted(df1$x)

Output

[1] TRUE

Example2

Live Demo

> y<-rnorm(20,1,0.5) > df2<-data.frame(y) > df2

Output

 y 1 0.5731483 2 1.2753959 3 1.3351612 4 1.9271030 5 1.0375696 6 0.4298899 7 1.2225022 8 0.8681973 9 0.8746253 10 1.4602984 11 0.4892610 12 1.4181656 13 0.4254027 14 0.9397925 15 -0.1129803 16 1.2659725 17 1.2444735 18 1.6010428 19 0.1310723 20 0.2720108

Example

> is.unsorted(df2$y)

Output

[1] TRUE

Example3

Live Demo

> z<-runif(20,2,5) > df3<-data.frame(z) > df3

Output

 z 1 4.426558 2 2.405967 3 2.294161 4 4.629669 5 3.286604 6 4.836352 7 4.003845 8 2.456922 9 2.374872 10 3.216140 11 4.987071 12 4.713011 13 3.014407 14 2.792747 15 3.951100 16 3.023335 17 3.129816 18 3.688158 19 2.078943 20 2.323125

Example

> is.unsorted(df3$z)

Output

[1] TRUE
Updated on: 2020-11-19T07:17:00+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements