Skip to content
Prev 384624 / 398525 Next

Can I pass the grouped portions of a dataframe/tibble to a function in dplyr

Hello,

You can pass a grouped tibble to a function with grouped_modify but the 
function must return a data.frame (or similar).

## this will also do it
#sillyFun <- function(tib){
#  tibble(nrow = nrow(tib), ncol = ncol(tib))
#}


sillyFun <- function(tib){
   data.frame(nrow = nrow(tib), ncol = ncol(tib)))
}

tib %>%
   group_by(y) %>%
   group_modify(~ sillyFun(.))
## A tibble: 3 x 3
## Groups:   y [3]
#      y  nrow  ncol
#  <dbl> <int> <int>
#1     1    17     2
#2     2    21     2
#3     3    12     2


Hope this helps,

Rui Barradas

?s 09:43 de 05/07/2020, Chris Evans escreveu: