Skip to content

Allow mutate() to choose the position of new columns #2047

@krlmlr

Description

@krlmlr
Member

These arguments would specify the position where the new columns are inserted, before or after some column given as index or name.

See also tidyverse/tibble#99.

Activity

krlmlr

krlmlr commented on Nov 7, 2016

@krlmlr
Author
hadley

hadley commented on Nov 7, 2016

@hadley
krlmlr

krlmlr commented on Nov 7, 2016

@krlmlr
Author
hadley

hadley commented on Feb 2, 2017

@hadley
krlmlr

krlmlr commented on Feb 2, 2017

@krlmlr
Author
lionel-

lionel- commented on Feb 2, 2017

@lionel-
hadley

hadley commented on Feb 2, 2017

@hadley
Member

To me, that seems overly complicated. I think 90% of the time people just want variables to go in the front, instead of in the back.

krlmlr

krlmlr commented on Feb 2, 2017

@krlmlr
Author
krlmlr

krlmlr commented on Mar 30, 2017

@krlmlr
Author
romainfrancois

romainfrancois commented on Mar 27, 2018

@romainfrancois
krlmlr

krlmlr commented on Mar 29, 2018

@krlmlr
MemberAuthor

If we're only interested in supporting adding to the front, maybe we could use transmute()?

@lionel-: What would it take to make the following syntax work?

library(tidyverse)
tibble(a = 2, b = 3) %>% transmute(x = 1, everything())
#> Error in mutate_impl(.data, dots): Evaluation error: No tidyselect variables were registered.

Created on 2018-03-30 by the reprex package (v0.2.0).

34 remaining items

batpigandme

batpigandme commented on Dec 5, 2019

@batpigandme
deleted a comment from Sibojang9 on Dec 5, 2019
deleted a comment from batpigandme on Dec 5, 2019
deleted a comment from Sibojang9 on Dec 5, 2019
braiam

braiam commented on Dec 30, 2019

@braiam

In MySQL we have two verbs, first and after , to add a column in a table with alter. Maybe dplyr can copy that behavior? It could also solve #4598.

hadley

hadley commented on Dec 31, 2019

@hadley
Member

I'm once again leaning towards a .where argument, which would use tidyselect along the lines of r-lib/tidyselect#151.

hadley

hadley commented on Dec 31, 2019

@hadley
Member

Discussion of dedicated move verb at #4598

You can now use transmute() + across() to add new cols at the start:

df %>%
  transmute(x = y ^ 2, across(everything())
changed the title [-]FR: `before` and `after` arguments to mutate()[/-] [+]Allow mutate() to choose the position of new columns[/+] on Jan 7, 2020
added a commit that references this issue on Jan 23, 2020
baf1168
courtiol

courtiol commented on Feb 8, 2020

@courtiol
Contributor

@hadley, this is a great commit, but perhaps it should support multiple columns relocation (the thread title is indeed Allow mutate() to choose the position of new columns):

iris %>%
  as_tibble() %>%
  mutate(Petal.Length.mm = Petal.Length * 10,
         Sepal.Length.mm = Sepal.Length * 10,
         .after = c(Petal.Length, Sepal.Length))

produces

# A tibble: 150 x 6
   Petal.Length Sepal.Length Petal.Length.mm Sepal.Length.mm Petal.Width Species

and not

# A tibble: 150 x 6
   Petal.Length Petal.Length.mm Sepal.Length Sepal.Length.mm Petal.Width Species
lionel-

lionel- commented on Feb 8, 2020

@lionel-
Member

@courtiol Your suggestion would not work with plural selections like starts_with() or is.numeric.

courtiol

courtiol commented on Feb 8, 2020

@courtiol
Contributor

I agree @lionel-, but it would make sense in mutate() calls without suffix (as shown above), wouldn't it?
Perhaps for mutate_if() and mutate_at() a possibility would be to consider a .before = .x (or .after = .x) to imply that each newly created column goes before (or after) its source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @hadley@batpigandme@braiam@krlmlr@romainfrancois

      Issue actions

        Allow mutate() to choose the position of new columns · Issue #2047 · tidyverse/dplyr