0

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?

Ian
  • 1,507
  • 3
  • 21
  • 36

3 Answers3

0

How about:

library(dplyr)
tribble(
  ~A,   ~N,
  "a",   1,
  "a",   2,
  "a",   3,
  "a",   4,
  "a",   5,
  "b",   1,  
  "b",   2
) %>%
  group_by(A) %>%
  summarise(xxx = n()) %>% 
  ungroup()
#  A       xxx
#  <chr> <int>
#1 a         5
#2 b         2

But honestly, wouldn't table be simplier?

c(table(tribble(
  ~A,   ~N,
  "a",   1,
  "a",   2,
  "a",   3,
  "a",   4,
  "a",   5,
  "b",   1,  
  "b",   2)$A))
#a b 
#5 2

Edit: Please also see How to return nrow per group in R?

Sascha
  • 194
  • 7
  • For this simplified case this would, of course, work. But what I'm trying to understand is how to invoke the anoymous function correctly and this data, and sample function, is a simplification purely to provide an MRE. – Ian Mar 27 '23 at 11:56
0

Solved on https://community.rstudio.com/t/how-to-call-anonymous-functions-with-summarise/163492

Should be pick(everything()) without the dot.

Ian
  • 1,507
  • 3
  • 21
  • 36
0

I had the same problem and I solved it by passing the tibble to data.frame