dplyr icon indicating copy to clipboard operation
dplyr copied to clipboard

Unexpected interaction between rowwise and group_modify

Open banbh opened this issue 2 years ago • 2 comments

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()

banbh avatar Jun 24 '23 19:06 banbh

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 avatar Jun 28 '23 12:06 hadley

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.

DavisVaughan avatar Jul 17 '23 13:07 DavisVaughan