[R] comparing characters

Martin Maechler maechler at stat.math.ethz.ch
Mon Nov 3 20:42:27 CET 2003


>>>>> "Giovanni" == Giovanni Petris <GPetris at uark.edu>
>>>>>     on Mon, 3 Nov 2003 12:00:41 -0600 (CST) writes:

    Giovanni> You could do something like that:

    >> x <- sample(LETTERS, 10) x
    Giovanni>  [1] "K" "N" "C" "F" "R" "E" "L" "J" "S" "Q"
    >> all.equal(order(x), 1:length(x))
    Giovanni> [1] "Mean relative difference: 0.5090909"

    Giovanni> When x is a numeric vector, I usually use

    >> any(diff(x) < 0)

    Giovanni> although I don't know whether it's more efficient.

There's a builtin R function that is definitely even faster
(at least for largish vectors):

is.unsorted()

--
Regards,
Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><




More information about the R-help mailing list