forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolwise-arrange.R
50 lines (50 loc) · 1.7 KB
/
colwise-arrange.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#' Arrange rows by a selection of variables
#'
#' These [scoped] variants of [arrange()] sort a data frame by a
#' selection of variables. Like [arrange()], you can modify the
#' variables before ordering with the `.funs` argument.
#'
#' @inheritParams scoped
#' @inheritParams arrange
#'
#' @section Grouping variables:
#'
#' The grouping variables that are part of the selection participate
#' in the sorting of the data frame.
#'
#' @export
#' @examples
#' df <- as_tibble(mtcars)
#' df
#' arrange_all(df)
#'
#' # You can supply a function that will be applied before taking the
#' # ordering of the variables. The variables of the sorted tibble
#' # keep their original values.
#' arrange_all(df, desc)
#' arrange_all(df, list(~desc(.)))
arrange_all <- function(.tbl, .funs = list(), ..., .by_group = FALSE) {
funs <- manip_all(.tbl, .funs, enquo(.funs), caller_env(), .include_group_vars = TRUE, ...)
if (!length(funs)) {
funs <- syms(tbl_vars(.tbl))
}
arrange(.tbl, !!!funs, .by_group = .by_group)
}
#' @rdname arrange_all
#' @export
arrange_at <- function(.tbl, .vars, .funs = list(), ..., .by_group = FALSE) {
funs <- manip_at(.tbl, .vars, .funs, enquo(.funs), caller_env(), .include_group_vars = TRUE, ...)
if (!length(funs)) {
funs <- tbl_at_syms(.tbl, .vars, .include_group_vars = TRUE)
}
arrange(.tbl, !!!funs, .by_group = .by_group)
}
#' @rdname arrange_all
#' @export
arrange_if <- function(.tbl, .predicate, .funs = list(), ..., .by_group = FALSE) {
funs <- manip_if(.tbl, .predicate, .funs, enquo(.funs), caller_env(), .include_group_vars = TRUE, ...)
if (!length(funs)) {
funs <- tbl_if_syms(.tbl, .predicate, .include_group_vars = TRUE)
}
arrange(.tbl, !!!funs, .by_group = .by_group)
}