dplyr
dplyr copied to clipboard
Unexpected interaction between rowwise and group_modify
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()
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
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.