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

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

Open
hughjonesd opened this issue Jun 21, 2024 · 0 comments
Open

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

hughjonesd opened this issue Jun 21, 2024 · 0 comments

Comments

@hughjonesd
Copy link

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.

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

No branches or pull requests

1 participant