This is a follow up to Reordering factor gives different results, depending on which packages are loaded, with another, related question.
@Andrie's answer is the correct one and following @David Lovell's comment, I am a third confused soul because of this.
In my case it was because I had loaded ROCR
, which depends on gplots
, which depends on gdata
, and I hadn't even heard of gdata
, to illustrate my ignorance, and therefore didn't think to search for it.
I've discovered another quirk, which made it even more difficult to work out in my case, and is the point of this question. Something about gdata:::reorder.factor
handles integers and numerics differently. To illustrate:
library(gdata)
x <- factor(letters[1:6])
y <- c(1,4,3,5,6,2)
z <- c(1.1,2.4,1.3,2.5,2.6,1.2)
stats:::reorder.default(x, y, function(X)-X) #edbcfa - correct
stats:::reorder.default(x, z, function(X)-X) #edbcfa
stats:::reorder.default(x, -y) #edbcfa
stats:::reorder.default(x, -z) #edbcfa
gdata:::reorder.factor(x, y, function(X)-X) #edbcfa
gdata:::reorder.factor(x, z, function(X)-X) #bdeafc - weird
gdata:::reorder.factor(x, -y) #abcdef - no reordering
gdata:::reorder.factor(x, -z) #abcdef - no reordering
It's mostly the bdeafc that I'm interested in. It has got the bit before the decimal correct, in that the 2.x are before the 1.x, but the part after the decimal point is in normal order, not reverse order: x.1 before x.2 before x.3.
Why is this?