I do not do amazing business on well-formulated questions. And I thought the code was readable and reasonable. If you want to tighten it up a bit, you can reset the paste () operation using "[[" and creating an index inside "[":
ReorderDataByColumn2 <- function(x, column) { return(x[ order( x[[column]]), ]) }
EDIT: adding a Hadley sentence (except, I think you also need do.call):
ReorderDataByColumn2 <- function(x, column, desc=FALSE) { return( x[ do.call( order, x[ , column, drop=FALSE ] ), ] ) }
You can add some errors if you want:
ReorderDataByColumn2 <- function(x, column) { if(column %in% names(x)){return(x[ order( x[[column]]), ]) }else{ cat("Column ", column, "not in dataframe ", deparse(substitute(x))) } }
source share