How to call anonymous functions with summarise.

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?

I believe your use of the . breaks your previous functionality
look at your original named function use , which doesnt feature the ., and if you added the . would break similarly.

You are absolutely correct! Many thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.