Skip to content

.by surprisingly doesn't work in mutate() #194

Description

@hughjonesd

In dplyr 1.1.0, the following groups data before performing a mutate:

iris |> mutate(.by = Species, n_in_species = n())

I expected the same to work in tidygraph, but in fact it works like the old interface:

library(tidygraph)
data(whigs, package = "ggraph")
whigs <- as_tbl_graph(whigs) |> activate(nodes) |> mutate(AM = grepl("^[A-M]", name))

whigs |> mutate(.by = AM,
  n_in_group = n()
)
...
   type  name               AM    .by   n_in_group
   <lgl> <chr>              <lgl> <lgl>      <int>
 1 FALSE John Adams         TRUE  TRUE         261
 2 FALSE Samuel Adams       FALSE FALSE        261

where the numbers should be more like:

whigs |> 
  group_by(AM) |> 
  mutate(.by = AM,
    n_in_group = n()
  ) |> 
  ungroup()
...
   type  name               AM    .by   n_in_group
   <lgl> <chr>              <lgl> <lgl>      <int>
 1 FALSE John Adams         TRUE  TRUE         152
 2 FALSE Samuel Adams       FALSE FALSE        109

This is a silent failure. Until dplyr .by is supported, it might be worth just throwing a warning when .by is used in a dplyr verb.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions