Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* R >=3.6.0 is now explicitly required (#7026).

* The new equivalent of `transmute()` is `mutate(.keep = "transmute")`. This is analogous to `mutate(.keep = "none")`, but it retains the column order within mutate (#6861).

# dplyr 1.1.4

* `join_by()` now allows its helper functions to be namespaced with `dplyr::`,
Expand Down
14 changes: 10 additions & 4 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand All @@ -187,6 +189,7 @@ mutate.data.frame <- function(.data,

out <- mutate_relocate(
out = out,
keep = keep,
before = {{ .before }},
after = {{ .after }},
names_original = names_original
Expand All @@ -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)
}

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions R/transmute.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
}
Expand Down
4 changes: 3 additions & 1 deletion man/mutate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/transmute.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.