[Rd] Expected behaviour of is.unsorted?

Richard Cotton richierocks at gmail.com
Wed May 23 12:23:39 CEST 2012


>> is.unsorted(data.frame(1:2))
> [1] FALSE
>> is.unsorted(data.frame(2:1))
> [1] FALSE
>> is.unsorted(data.frame(1:2,3:4))
> [1] TRUE
>> is.unsorted(data.frame(2:1,4:3))
> [1] TRUE
>
> IIUC, is.unsorted is intended for atomic vectors only (description of x in
> ?is.unsorted). Indeed the C source (src/main/sort.c) contains an error
> message "only atomic vectors can be tested to be sorted". So that is the
> error message I expected to see in all cases above, since I know that
> data.frame is not an atomic vector. But there is also this in
> ?is.unsorted: "except for atomic vectors and objects with a class (where
> the >= or > method is used)" which I don't understand. Where >= or > is
> used by what, and where?
>
> I understand why the first two are FALSE (1 item of anything must be
> sorted). I don't understand the 3rd and 4th cases where length is 2:
> do_isunsorted seems to call lang3(install(".gtn"), x, CADR(args))). Does
> that fall back to TRUE for some reason?

I've just been having similar worries with this today.  The odd
behaviour seems to be particular to data.frames.  Compare for example,

is.unsorted(list(1, 3, 2))              #NA
is.unsorted(data.frame(1, 3, 2)) #FALSE
is.unsorted(data.frame(1, 2, 3)) #TRUE

IMHO, it would be clearer if is.unsorted either always returned NA for
recursive objects of length 2 or more, or it called unlist to make it
atomic.  Either way, it should really provide some sort of warning
about non-standard input.

-- 
Regards,
Richie

live-analytics.com
4dpiecharts.com



More information about the R-devel mailing list