[R] What is the function to test if a vector is ordered or not?

Marc Schwartz marc_schwartz at me.com
Thu Dec 10 05:21:47 CET 2009


On Dec 9, 2009, at 10:10 PM, Peng Yu wrote:

> I did a search on www.rseek.org to look for the function to test if a
> vector is ordered or not. But I don't find it. Could somebody let me
> know what function I should use?


If by ordered, you mean sorted, then ?is.unsorted

 > is.unsorted(c(1, 4, 2, 6, 7))
[1] TRUE

 > is.unsorted(sort(c(1, 4, 2, 6, 7)))
[1] FALSE


If you mean to test a factor to see if it is an ordered factor, then ? 
is.ordered

 > is.ordered(factor(letters))
[1] FALSE

 > is.ordered(factor(letters, ordered = TRUE))
[1] TRUE


HTH,

Marc Schwartz




More information about the R-help mailing list