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

Unexpected interaction between rowwise and group_modify #6870

Open
banbh opened this issue Jun 24, 2023 · 2 comments
Open

Unexpected interaction between rowwise and group_modify #6870

banbh opened this issue Jun 24, 2023 · 2 comments
Labels
bug an unexpected problem or unintended behavior grouping 👨‍👩‍👧‍👦

Comments

@banbh
Copy link

banbh commented Jun 24, 2023

It appears that after an application of rowwise the behavior of group_modify differs from that of group_walk and group_map; the latter two behave as expected. For example, I expected all three pipelines to print two "hello"s, but the group_modify one only prints one.

library(dplyr) # dplyr_1.1.2

X <- tibble(a = 1:2)

f <- \(...) {
  print('hello')
  tibble(x = runif(3))
}

X |> 
  rowwise() |> 
  group_walk(f) # two hellos

X |> 
  rowwise() |> 
  group_map(f) |> # two hellos
  invisible()

X |> 
  rowwise() |> 
  group_modify(f) |> # one hello ???
  invisible()
@hadley
Copy link
Member

hadley commented Jun 28, 2023

Slightly smaller/more illustrative reprex:

library(dplyr, warn.conflicts = FALSE)

df <- tibble(a = 1:2)

df |> 
  rowwise() |> 
  group_modify(\(key, grp) tibble(rows = nrow(grp)))
#> # A tibble: 1 × 1
#>    rows
#>   <int>
#> 1     2

Created on 2023-06-28 with reprex v2.0.2

@hadley hadley added bug an unexpected problem or unintended behavior grouping 👨‍👩‍👧‍👦 labels Jun 28, 2023
@DavisVaughan
Copy link
Member

It looks like we may need group_modify.rowwise_df(). Possibly it can be the same as group_modify.grouped_df(), since that looks to be mostly generic.

That said, group_split.rowwise_df() ignores .keep, so maybe group_modify.rowwise_df() should too. Not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior grouping 👨‍👩‍👧‍👦
Projects
None yet
Development

No branches or pull requests

3 participants