R/df.sortcol.R

Defines functions df.sortcol

Documented in df.sortcol

###############################################################################################
##### sort a data frame by a column
###############################################################################################

#' @name df.sortcol
#' @aliases df.sortcol
#' @title Sort a data frame by a column
#' @description Sort a data frame by a column of choice. The column of choice is specified by the number of the column.
#' @usage df.sortcol(x,n,desc)
#' @param x :a data frame
#' @param n :number column to sort
#' @param desc :the order of sorting, default set to TRUE; for ascending order set to FALSE
#' @examples df.sortcol(mtcars,2,desc = TRUE)

df.sortcol <- function(x, n, desc = T){

  if(desc == FALSE){

    x[order(x[,n]), ]

  }else{

    x[order(x[,n], decreasing = T), ]

  }

}

Try the YRmisc package in your browser

Any scripts or data that you put into this service are public.

YRmisc documentation built on March 25, 2020, 5:13 p.m.