It Has Always Been Wrong to Call order on a data.frame

[This article was first published on R – Win Vector LLC, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R it has always been incorrect to call order() on a data.frame. Such a call doesn’t return a sort-order of the rows, and previously did not return an error. For example.

d <- data.frame(
  x = c(2, 2, 3, 3, 1, 1), 
  y = 6:1)

knitr::kable(d)
x y
2 6
2 5
3 4
3 3
1 2
1 1
order(d)
##  [1]  5  6 12  1  2 11  3  4 10  9  8  7

Notice the above result has more than 6 items, so it is not a row order. It appears there is a desire to make this sort of mal-use signalling, and it is now available as an optional error-check. In fact we are starting to see packages kicked-off CRAN for not fixing this issue.

Recent CRAN package removals (from CRANberries, triggered by failing to respond when contacted to fix the order() error, (error resolves as “cannot xtfrm data frames”) include:

The wrapr package has supplied, for some time, the function orderv(), which is suitable for ordering the rows of data.frames.

For example, we can calculate a row order as follows.

library(wrapr)

orderv(d)
## [1] 6 5 2 1 4 3

And use such an order to sort data rows.

d[orderv(d), , drop = FALSE] %.>%
  knitr::kable(.)
x y
6 1 1
5 1 2
2 2 5
1 2 6
4 3 3
3 3 4

Essentially orderv(d) is shorthand for do.call(base::order, as.list(d)), which places the columns of the data.frame as the ...-arguments of the order() call.

Edit: an earlier great fix can be found here.

To leave a comment for the author, please follow the link and comment on their blog: R – Win Vector LLC.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)