|
1 | | -#' Change column order |
2 | | -#' |
3 | | -#' Use `relocate()` to change column positions, using the same syntax as |
4 | | -#' `select()` to make it easy to move blocks of columns at once. |
5 | | -#' |
6 | | -#' @inheritParams fill.polars_data_frame |
7 | | -#' @param .before,.after Column name (either quoted or unquoted) that |
8 | | -#' indicates the destination of columns selected by `...`. Supplying neither |
9 | | -#' will move columns to the left-hand side; specifying both is an error. |
10 | | -#' |
11 | | -#' @export |
12 | | -#' @examplesIf require("dplyr", quietly = TRUE) && require("tidyr", quietly = TRUE) |
13 | | -#' dat <- as_polars_df(mtcars) |
14 | | -#' |
15 | | -#' dat |> |
16 | | -#' relocate(hp, vs, .before = cyl) |
17 | | -#' |
18 | | -#' # if .before and .after are not specified, selected columns are moved to the |
19 | | -#' # first positions |
20 | | -#' dat |> |
21 | | -#' relocate(hp, vs) |
22 | | -#' |
23 | | -#' # .before and .after can be quoted or unquoted |
24 | | -#' dat |> |
25 | | -#' relocate(hp, vs, .after = "gear") |
26 | | -#' |
27 | | -#' # select helpers are also available |
28 | | -#' dat |> |
29 | | -#' relocate(contains("[aeiou]")) |
30 | | -#' |
31 | | -#' dat |> |
32 | | -#' relocate(hp, vs, .after = last_col()) |
33 | | - |
34 | | -relocate.polars_data_frame <- function( |
35 | | - .data, |
36 | | - ..., |
37 | | - .before = NULL, |
38 | | - .after = NULL |
39 | | -) { |
40 | | - if (!missing(.before) && !missing(.after)) { |
41 | | - cli_abort( |
42 | | - "You can specify either {.code .before} or {.code .after} but not both." |
43 | | - ) |
44 | | - } |
45 | | - |
46 | | - names_data <- names(.data) |
47 | | - |
48 | | - if (missing(.before) && missing(.after)) { |
49 | | - .before <- names_data[1] |
50 | | - where <- "BEFORE" |
51 | | - } else if (!missing(.before) && missing(.after)) { |
52 | | - .before <- tidyselect_named_arg(.data, rlang::enquo(.before)) |
53 | | - where <- "BEFORE" |
54 | | - } else if (missing(.before) && !missing(.after)) { |
55 | | - .after <- tidyselect_named_arg(.data, rlang::enquo(.after)) |
56 | | - where <- "AFTER" |
57 | | - } |
58 | | - |
59 | | - vars <- tidyselect_dots(.data, ...) |
60 | | - if (length(vars) == 0) { |
61 | | - return(add_tidypolars_class(.data)) |
62 | | - } |
63 | | - |
64 | | - not_moving <- setdiff(names_data, vars) |
65 | | - |
66 | | - if (where == "BEFORE") { |
67 | | - limit <- if (is.null(.before)) { |
68 | | - 0 |
69 | | - } else { |
70 | | - which(names_data == .before[1]) - 1 |
71 | | - } |
72 | | - lhs <- names_data[seq_len(limit)] |
73 | | - lhs <- lhs[which(lhs %in% not_moving)] |
74 | | - rhs <- names_data[seq(limit + 1, ncol(.data))] |
75 | | - rhs <- rhs[which(rhs %in% not_moving)] |
76 | | - new_order <- c(lhs, vars, rhs) |
77 | | - } else if (where == "AFTER") { |
78 | | - limit <- if (is.null(.after)) { |
79 | | - ncol(.data) |
80 | | - } else { |
81 | | - which(names_data == .after[length(.after)]) |
82 | | - } |
83 | | - lhs <- names_data[seq_len(limit)] |
84 | | - lhs <- lhs[which(lhs %in% not_moving)] |
85 | | - # we don't have RHS if we relocate columns to be in the last position |
86 | | - if (identical(lhs, not_moving)) { |
87 | | - rhs <- NULL |
88 | | - } else { |
89 | | - rhs <- names_data[seq(limit + 1, ncol(.data))] |
90 | | - rhs <- rhs[which(rhs %in% not_moving)] |
91 | | - } |
92 | | - new_order <- c(lhs, vars, rhs) |
93 | | - } |
94 | | - |
95 | | - out <- .data$select(!!!new_order) |
96 | | - add_tidypolars_class(out) |
97 | | -} |
98 | | - |
99 | | -#' @rdname relocate.polars_data_frame |
100 | | -#' @export |
101 | | -relocate.polars_lazy_frame <- relocate.polars_data_frame |
| 1 | +#' Change column order |
| 2 | +#' |
| 3 | +#' Use `relocate()` to change column positions, using the same syntax as |
| 4 | +#' `select()` to make it easy to move blocks of columns at once. |
| 5 | +#' |
| 6 | +#' @inheritParams fill.polars_data_frame |
| 7 | +#' @param .before,.after Column name (either quoted or unquoted) that |
| 8 | +#' indicates the destination of columns selected by `...`. Supplying neither |
| 9 | +#' will move columns to the left-hand side; specifying both is an error. |
| 10 | +#' |
| 11 | +#' @export |
| 12 | +#' @examplesIf require("dplyr", quietly = TRUE) && require("tidyr", quietly = TRUE) |
| 13 | +#' dat <- as_polars_df(mtcars) |
| 14 | +#' |
| 15 | +#' dat |> |
| 16 | +#' relocate(hp, vs, .before = cyl) |
| 17 | +#' |
| 18 | +#' # if .before and .after are not specified, selected columns are moved to the |
| 19 | +#' # first positions |
| 20 | +#' dat |> |
| 21 | +#' relocate(hp, vs) |
| 22 | +#' |
| 23 | +#' # .before and .after can be quoted or unquoted |
| 24 | +#' dat |> |
| 25 | +#' relocate(hp, vs, .after = "gear") |
| 26 | +#' |
| 27 | +#' # select helpers are also available |
| 28 | +#' dat |> |
| 29 | +#' relocate(contains("[aeiou]")) |
| 30 | +#' |
| 31 | +#' dat |> |
| 32 | +#' relocate(hp, vs, .after = last_col()) |
| 33 | + |
| 34 | +relocate.polars_data_frame <- function( |
| 35 | + .data, |
| 36 | + ..., |
| 37 | + .before = NULL, |
| 38 | + .after = NULL |
| 39 | +) { |
| 40 | + if (!missing(.before) && !missing(.after)) { |
| 41 | + cli_abort( |
| 42 | + "You can specify either {.code .before} or {.code .after} but not both." |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + names_data <- names(.data) |
| 47 | + |
| 48 | + if (missing(.before) && missing(.after)) { |
| 49 | + .before <- names_data[1] |
| 50 | + where <- "BEFORE" |
| 51 | + } else if (!missing(.before) && missing(.after)) { |
| 52 | + .before <- tidyselect_named_arg(.data, rlang::enquo(.before)) |
| 53 | + where <- "BEFORE" |
| 54 | + } else if (missing(.before) && !missing(.after)) { |
| 55 | + .after <- tidyselect_named_arg(.data, rlang::enquo(.after)) |
| 56 | + where <- "AFTER" |
| 57 | + } |
| 58 | + |
| 59 | + vars <- tidyselect_dots(.data, ...) |
| 60 | + if (length(vars) == 0) { |
| 61 | + return(add_tidypolars_class(.data)) |
| 62 | + } |
| 63 | + |
| 64 | + not_moving <- setdiff(names_data, vars) |
| 65 | + |
| 66 | + if (where == "BEFORE") { |
| 67 | + limit <- if (is.null(.before)) { |
| 68 | + 0 |
| 69 | + } else { |
| 70 | + which(names_data == .before[1]) - 1 |
| 71 | + } |
| 72 | + lhs <- names_data[seq_len(limit)] |
| 73 | + lhs <- lhs[which(lhs %in% not_moving)] |
| 74 | + rhs <- names_data[seq(limit + 1, ncol(.data))] |
| 75 | + rhs <- rhs[which(rhs %in% not_moving)] |
| 76 | + new_order <- c(lhs, vars, rhs) |
| 77 | + } else if (where == "AFTER") { |
| 78 | + limit <- if (is.null(.after)) { |
| 79 | + ncol(.data) |
| 80 | + } else { |
| 81 | + which(names_data == .after[length(.after)]) |
| 82 | + } |
| 83 | + lhs <- names_data[seq_len(limit)] |
| 84 | + lhs <- lhs[which(lhs %in% not_moving)] |
| 85 | + # we don't have RHS if we relocate columns to be in the last position |
| 86 | + if (identical(lhs, not_moving)) { |
| 87 | + rhs <- NULL |
| 88 | + } else { |
| 89 | + rhs <- names_data[seq(limit + 1, ncol(.data))] |
| 90 | + rhs <- rhs[which(rhs %in% not_moving)] |
| 91 | + } |
| 92 | + new_order <- c(lhs, vars, rhs) |
| 93 | + } |
| 94 | + |
| 95 | + out <- .data$select(!!!new_order) |
| 96 | + add_tidypolars_class(out) |
| 97 | +} |
| 98 | + |
| 99 | +#' @rdname relocate.polars_data_frame |
| 100 | +#' @export |
| 101 | +relocate.polars_lazy_frame <- relocate.polars_data_frame |
0 commit comments