[R] lexicographic comparison of two vectors

From: Gabriel Valiente <valiente_at_lsi.upc.edu>
Date: Mon, 12 May 2008 20:58:45 +0200

Is there any built-in way to lexicographically compare two vectors of the same length in R? The textbook algorithm could be coded as follows:

lex.cmp <- function (vec1,vec2) {

   for (j in 1:length(vec1)) {
     if (vec1[j] < vec2[j]) { return(-1) }
     if (vec1[j] > vec2[j]) { return(1) }
   }
   return(0)

}

Thanks,

Gabriel



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 12 May 2008 - 19:08:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 12 May 2008 - 20:32:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive