开发者

Is there a simpler way to reorder data by the values of a column?

I wrote this tiny little wrapper around order, b开发者_开发技巧ut I fear my implementation is lame. I huddle in the corner, waiting for the gods of R commands or algorithmic efficiency to smite my ergonomic keyboard :-(

set.seed(1001)

height <- rnorm(6, mean = 1, sd = 0.2)
weight <- rnorm(6, mean = 100, sd = 15)
id     <- 1:6

dd <- data.frame(id, height, weight)

# Here's the function I came up with
ReorderDataByColumn <- function(x, column) {
  ordered.indices <- order(x[ ,paste(column)])

  return(x[ordered.indices, ])
}

#And here are its results
> ReorderDataByColumn(dd, column = "height")
  id    height    weight
4  4 0.4986928  76.09430
5  5 0.8885377 104.53967
3  3 0.9629449  86.38809
2  2 0.9644905  90.65584
6  6 0.9712881 124.51589
1  1 1.4377296 116.37253

> ReorderDataByColumn(dd, column = "weight")
  id    height    weight
4  4 0.4986928  76.09430
3  3 0.9629449  86.38809
2  2 0.9644905  90.65584
5  5 0.8885377 104.53967
1  1 1.4377296 116.37253
6  6 0.9712881 124.51589


I'm not into the smiting business for well-formed questions. And I thought the code was readable and sensible. If you wanted to tighten it up a bit you can drop the paste() operation by using "[[" and creating the index inside "[":

ReorderDataByColumn2 <- function(x, column) {
    return(x[ order( x[[column]]), ])
}

EDIT: Adding Hadley's suggestion (except I think you need do.call as well):

 ReorderDataByColumn2 <- function(x, column, desc=FALSE) {
    return(
      x[ do.call( order, x[ , column, drop=FALSE ]  ), ]
      ) }

You could add some error checking if you wanted:

ReorderDataByColumn2 <- function(x, column) {
    if(column %in% names(x)){return(x[ order( x[[column]]), ]) 
     }else{ cat("Column ", column, "not in dataframe ", deparse(substitute(x))) }
}


See the arrange function in plyr:

library(plyr)
arrange(mtcars, cyl)
arrange(mtcars, desc(cyl))
arrange(mtcars, vs, am)

The definition of the function is pretty simple:

arrange <- function (df, ...) {
    ord <- eval(substitute(order(...)), df, parent.frame())
    unrowname(df[ord, ])
}

And it works on a very similar process to subset in base R.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜