Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.keep = "transmute" is implemented in mutate. #7038

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

apalacio9502
Copy link

@apalacio9502 apalacio9502 commented Jun 2, 2024

Hi @hadley and @DavisVaughan,

This merge request aims to align the mutate functionality with that of transmute. Presently, the documentation states that the equivalent of transmute is mutate(.keep="none"). However, it's important to note that this function retains the column order, unlike transmute.

After reviewing the bug reported here: #6861, I concur that there should be a fifth variant of keep, named "transmute."

This proposal should also be implemented in the following tidyverse libraries:

And in the following third party libraries:

This change opens the possibility of un-superseded transmute. For example, you could implement that when a user calls transmute, it internally executes mutate(.keep="transmute"). This would greatly benefit veteran users who have extensively used transmute in their projects. Additionally, it improves code readability, as a simple transmute call is clearer than mutate(.keep="transmute").

Below I leave an example comparing transmute with mutate(.keep = "transmute")

example <- tibble(
  group = c("a", "a", "b", "b"),
  old = c(1, 2, 3, 4)
)

# transmute --------------------------------------------------------------------

example %>% transmute(
  group,
  new = old * 2,
  old
)

# A tibble: 4 × 3
  group   new   old
  <chr> <dbl> <dbl>
1 a         2     1
2 a         4     2
3 b         6     3
4 b         8     4

# mutate(.keep = "transmute") --------------------------------------------------

example %>% mutate(
  .keep = "transmute",
  group,
  new = old * 2,
  old
)

# A tibble: 4 × 3
  group   new   old
  <chr> <dbl> <dbl>
1 a         2     1
2 a         4     2
3 b         6     3
4 b         8     4

# group_by + transmute ---------------------------------------------------------

example %>% 
  group_by(group) %>% 
  transmute(
    new = sum(old),
    old
) %>% 
 ungroup()

# A tibble: 4 × 3
  group   new   old
  <chr> <dbl> <dbl>
1 a         3     1
2 a         3     2
3 b         7     3
4 b         7     4

# group_by + mutate(.keep = "transmute") ---------------------------------------

example %>% 
  group_by(group) %>% 
  mutate(
    .keep = "transmute",
    new = sum(old),
    old
) %>% 
 ungroup()

# A tibble: 4 × 3
  group   new   old
  <chr> <dbl> <dbl>
1 a         3     1
2 a         3     2
3 b         7     3
4 b         7     4

# mutate(.keep = "transmute", by = ...) ----------------------------------------

example %>%  
  mutate(
    .keep = "transmute",
    .by = "group",
    new = sum(old),
    old
  )

# A tibble: 4 × 3
  group   new   old
  <chr> <dbl> <dbl>
1 a         3     1
2 a         3     2
3 b         7     3
4 b         7     4

Regards,

@DavisVaughan
Copy link
Member

We may simply un-supercede transmute() since many people seem to like it and have pushed back on that, in which case we probably would not add this because it does have very different properties than the theoretically consistent set of existing .keep options. I am not sure yet though, we will need to have an internal discussion about it and we aren't currently doing a large amount of dplyr dev at the moment.

@apalacio9502
Copy link
Author

apalacio9502 commented Jun 3, 2024

Hi @DavisVaughan,

Thank you for your response and I appreciate your perspective.

From my standpoint as a user, I think it would be beneficial to consider the following alternative.

  • Incorporating the parameter mutate(.keep = "transmute"): I am convinced that adding this parameter would enrich the process, providing greater flexibility. This would allow for quick adaptation of the outcome through the option .keep = c("all", "used", "unused", "none", "transmute"). If we only opt for "un-superseded transmute" and a user wishes to transform a "mutate" into a "transmute", it would entail changing the function name and removing parameters that are not compatible with "transmute".

  • Un-superseded transmute: This would be valuable for those users who prefer using transmute due to its clarity in the code, or for those who encounter difficulties when migrating to mutate(.keep = "transmute"), as it involves more than just a direct replacement (for example, if one wishes to add .keep at the end of the code or adopt a particular style). Additionally, I believe it would be interesting to include the parameter .by in transmute, which would add value for users.

I look forward to your comments.

Regards,

@DavisVaughan
Copy link
Member

Additionally, I believe it would be interesting to include the parameter .by in transmute, which would add value for users.

If we unsupercede, definitely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants