I'm trying to understand how to use anonymous functions with summarise.
For the sake of providing a simple MRE, if I do:
tribble(
~A, ~N,
"a", 1,
"a", 2,
"a", 3,
"a", 4,
"a", 5,
"b", 1,
"b", 2
) %>%
group_by(A) %>%
summarise( xxx = xxx(pick(everything())) )
where:
xxx = function(df) {
nrow(df)
}
(Please note this is only an example for the sake of the excercise, calculating the size is not the problem I'm trying to solve :))
I get the expected result:
# A tibble: 2 × 2
A xxx
<chr> <int>
1 a 5
2 b 2
But if I instead I use the anonymous function:
summarise( xxx = (function(x) {nrow(x)})(pick(everything(.))) )
I get the error:
Error in `summarise()`:
ℹ In argument: `xxx = (function(x) {...`.
Caused by error in `pick()`:
! Can't subset columns past the end.
ℹ Location 2 doesn't exist.
ℹ There is only 1 column.
Backtrace:
1. ... %>% ...
19. vctrs (local) `<fn>`()
20. vctrs:::stop_subscript_oob(...)
21. vctrs:::stop_subscript(...)
Yet if I don't use the pick(everything(.)), but just:
summarise( xxx = (function(x) {nrow(x)})(.) )
I don't get the divisions per group:
# A tibble: 2 × 2
A xxx
<chr> <int>
1 a 7
2 b 7
How do I correctly invoke an anonymous function passing just the grouped data?
Solved on https://community.rstudio.com/t/how-to-call-anonymous-functions-with-summarise/163492
Should be pick(everything())
without the dot.