1. '%-%' <- function(x,y) max(x-y,0) 2. globalSort <- function(xname) { tmp <- sort(get(xname,envir=.GlobalEnv)) assign(xname,tmp,envir=.GlobalEnv) } 3. dfsort <- function(df,col) { if (is.numeric(col)) { toSort <- df[,col] } else toSort <- df[[col]] # or replace above by df[[col]] tmp <- order(toSort) df[tmp,] }