[Rd] order(decreasing=c(TRUE,FALSE),...)

William Dunlap wdunl@p @ending from tibco@com
Tue Dec 4 21:02:48 CET 2018


The NEWS file for R-devel (as of  2018-11-28 r75702) says

 • order(...., decreasing=c(TRUE,FALSE)) could fail in some cases.
    Reported from StackOverflow via Karl Nordström.

However, either I don't understand the meaning of decreasing=c(TRUE,FALSE)
or there are still problems.  I thought order(x,y,decreasing=c(TRUE,FALSE)
meant to return indices, i, such that x[i] was non-increasing and that ties
among the x's would be broken by y in non-decreasing order.  E.g., that
interpretation works for numeric vectors:
  > d <- data.frame(X=c(2,1,2,1,2,2,1), N=c(4:7,1:3))
  > d[order(d$X, d$N, decreasing=c(TRUE, FALSE)), ] # expect decreasing X
and, within group of tied Xes, increasing N
    X N
  5 2 1
  6 2 2
  1 2 4
  3 2 6
  7 1 3
  2 1 5
  4 1 7
But it fails for character vectors:  E.g., add some of those that have the
same sort order as 'N':

  > d$Char <- LETTERS[d$N]
  > identical(order(d$N), order(d$Char)) # expect TRUE
  [1] TRUE

I expected the new columns to give the same sort order when they replace
'd$N' in the first call to order, but they do not:  It acts like it would
with decreasing=c(TRUE,TRUE).

  > order(d$X, d$Char, decreasing=c(TRUE, FALSE))
  [1] 3 1 6 5 4 2 7
  > d[order(d$X, d$Char, decreasing=c(TRUE, FALSE)), ]
    X N Char
  3 2 6    F
  1 2 4    D
  6 2 2    B
  5 2 1    A
  4 1 7    G
  2 1 5    E
  7 1 3    C

Bill Dunlap
TIBCO Software
wdunlap tibco.com

	[[alternative HTML version deleted]]



More information about the R-devel mailing list