R/desc.R

Defines functions asc desc

Documented in desc

#' Helpers to sort variables in ascending or descending order
#'
#' @description An alternative to `dplyr::desc()` which is much faster
#' for character vectors and factors.
#'
#' @param x Vector.
#'
#' @returns
#' A numeric vector that can be ordered in ascending or descending order. \cr
#' Useful in `dplyr::arrange()` or `f_arrange()`.
#'
#' @rdname desc
#' @export
desc <- function(x){
  -asc(x)
}
asc <- function(x){
  if (is.numeric(x) && !is.object(x)){
    x
  } else {
    strip_attrs(quick_group(x, order = TRUE, na_exclude = TRUE))
  }
}

Try the fastplyr package in your browser

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

fastplyr documentation built on June 8, 2025, 11:18 a.m.