When the “reorder” function just isn’t good enough…

The reorder function, in R 3.0.0, is behaving strangely (or I’m really not understanding something).  Take the following simple data frame:

df = data.frame(a1 = c(4,1,1,3,2,4,2), a2 = c(“h”,”j”,”j”,”e”,”c”,”h”,”c”))

I expect that if I call the reorder function on the a2 vector, using the a1 vector as the vector to order the second one by, then any summary stats that I run on the a2 vector will be ordered according to the numbers in a1.  However, look what happens:

table(reorder(df$a2, df$a1))
c e h j 
2 1 2 2

I found out that in order to get it in the order specified by the numbers in the first vector, the following code seems to work:

df$a2 = factor(df$a2, levels=unique(df$a2)[order(unique(df$a1))], ordered=TRUE)

Now look at the result:


j c e h 
2 2 1 2

One thing I notice here is that R seems to be keeping the factor levels alphabetically organized. When I specify the levels by using the “unique” function, it allows itself to break the alphabetic organization.

Why won’t the reorder function work in this case?


11 thoughts on “When the “reorder” function just isn’t good enough…

  1. (Previous comment seems to have been eaten up.)

    It works fine here using R 3.0.0/Mac: the output is your second result.

    • Ok so you probably have a conflict with either the gdata or the Hmisc package.
      Try running :
      reorder = stats:::reorder.default
      or directly : table(stats:::reorder.default(df$a2, df$a1))
      This ought to be working.

      Good to know though !


      • I’m curious to try this method of more directly referring to the reorder function from the default stats package. I swear, I don’t get this issue on my R 2.5.2 installation on my ubuntu box at home… Just my work computer!

      • I only had the issue after loading gdata. My R 3.0.0 gave the same (right) results on start-up.

      • Sir, you are a genius!! You figured out my problem 🙂 When I restart R and use the reorder command it works just fine. Then when I load gdata up I get the same problem I posted about.

        Thank you!!

      • David was a better investigator than me. I followed a lead that it might have been the reorder() function from the memisc package, but I missed the one from gdata.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s