Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Determine if a vector is ordered

$
0
0

I would like to determine if a vector is either always increasing or always decreasing in R.

Ideally, if I had these three vectors:

asc=c(1,2,3,4,5)
des=c(5,4,3,2,1)
non=c(1,3,5,4,2)

I would hope that the first two would return TRUE, and the last would return FALSE.

I tried a few approaches. First, I tried:

> is.ordered(asc)
[1] FALSE
> is.ordered(des)
[1] FALSE
> is.ordered(non)
[1] FALSE

And I also tried:

> order(non)
[1] 1 5 2 4 3

And hoped that I could simply compare this vector with 1,2,3,4,5 and 5,4,3,2,1, but even that returns a string of logicals, rather than a single true or false:

> order(non)==c(1,2,3,4,5)
[1]  TRUE FALSE FALSE  TRUE FALSE

Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>