diff --git a/R/mutate.R b/R/mutate.R index 7e4e546e8f..af21e6aea8 100644 --- a/R/mutate.R +++ b/R/mutate.R @@ -163,6 +163,8 @@ mutate <- function(.data, ...) { #' the columns used to generate them. #' * `"none"` doesn't retain any extra columns from `.data`. Only the grouping #' variables and columns created by `...` are kept. +#' * `"transmute"` equivalent to "none," but preserves the column order in the +#' mutate. #' @param .before,.after #' <[`tidy-select`][dplyr_tidy_select]> Optionally, control where new columns #' should appear (the default is to add to the right hand side). See @@ -171,10 +173,10 @@ mutate <- function(.data, ...) { mutate.data.frame <- function(.data, ..., .by = NULL, - .keep = c("all", "used", "unused", "none"), + .keep = c("all", "used", "unused", "none", "transmute"), .before = NULL, .after = NULL) { - keep <- arg_match0(.keep, values = c("all", "used", "unused", "none")) + keep <- arg_match0(.keep, values = c("all", "used", "unused", "none", "transmute")) by <- compute_by({{ .by }}, .data, by_arg = ".by", data_arg = ".data") @@ -187,6 +189,7 @@ mutate.data.frame <- function(.data, out <- mutate_relocate( out = out, + keep = keep, before = {{ .before }}, after = {{ .after }}, names_original = names_original @@ -208,11 +211,11 @@ mutate.data.frame <- function(.data, # Helpers ----------------------------------------------------------------- -mutate_relocate <- function(out, before, after, names_original) { +mutate_relocate <- function(out, keep, before, after, names_original) { before <- enquo(before) after <- enquo(after) - if (quo_is_null(before) && quo_is_null(after)) { + if (keep == "transmute" || (quo_is_null(before) && quo_is_null(after))) { return(out) } @@ -234,6 +237,9 @@ mutate_keep <- function(out, keep, used, names_new, names_groups) { if (keep == "all") { names_out <- names + } else if (keep == "transmute") { + names_groups <- setdiff(names_groups, names_new) + names_out <- c(names_groups, names_new) } else { names_keep <- switch( keep, diff --git a/R/transmute.R b/R/transmute.R index 106cde9860..81ced2470e 100644 --- a/R/transmute.R +++ b/R/transmute.R @@ -5,7 +5,7 @@ #' #' `transmute()` creates a new data frame containing only the specified #' computations. It's superseded because you can perform the same job -#' with `mutate(.keep = "none")`. +#' with `mutate(.keep = "transmute")`. #' #' @inheritParams mutate #' @section Methods: @@ -29,7 +29,7 @@ #' @export transmute <- function(.data, ...) { # dplyr 1.1.0 - lifecycle::signal_stage("superseded", "transmute()", I("mutate(.keep = 'none')")) + lifecycle::signal_stage("superseded", "transmute()", I("mutate(.keep = 'transmute')")) UseMethod("transmute") } diff --git a/man/mutate.Rd b/man/mutate.Rd index 1b2e7aa7c5..75d1328a09 100644 --- a/man/mutate.Rd +++ b/man/mutate.Rd @@ -11,7 +11,7 @@ mutate(.data, ...) .data, ..., .by = NULL, - .keep = c("all", "used", "unused", "none"), + .keep = c("all", "used", "unused", "none", "transmute"), .before = NULL, .after = NULL ) @@ -51,6 +51,8 @@ columns. This is useful if you generate new columns, but no longer need the columns used to generate them. \item \code{"none"} doesn't retain any extra columns from \code{.data}. Only the grouping variables and columns created by \code{...} are kept. +\item \code{"transmute"} equivalent to "none," but preserves the column order in the +mutate. }} \item{.before, .after}{<\code{\link[=dplyr_tidy_select]{tidy-select}}> Optionally, control where new columns diff --git a/man/transmute.Rd b/man/transmute.Rd index 4bdaea5b86..91895e21e1 100644 --- a/man/transmute.Rd +++ b/man/transmute.Rd @@ -41,7 +41,7 @@ specified by \code{...}. \code{transmute()} creates a new data frame containing only the specified computations. It's superseded because you can perform the same job -with \code{mutate(.keep = "none")}. +with \code{mutate(.keep = "transmute")}. } \section{Methods}{