[R] lexicographic comparison of two vectors

Gabriel Valiente valiente at lsi.upc.edu
Mon May 12 20:58:45 CEST 2008


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



More information about the R-help mailing list