cur_data help trying to apply a function to a column


Just wondering how to apply a simple mean and quantiles to each of the groups listed below? It comes back as a nested column, which is what I want and just apply a mean to it without using the summarize across.

df <- tibble(
  grp = rep(1:2, each = 5), 
  x = c(rnorm(5, -0.25, 1), rnorm(5, 0, 1.5)),
  y = c(rnorm(5, 0.25, 1), rnorm(5, 0, 0.5)),

df %>% 
  group_by(grp) %>% 
  summarize(Totals = list(cur_data()))

I'm relative new and still experimenting with dplyr.

I think is equivalent to

df %>% nest_by(grp) 

Is there something distasteful about ...

df %>%
  group_by(grp) %>%
  summarise(across(c(x, y), list(
    mean = ~ mean(.),
    p25 = ~ quantile(., .25),
    p75 = ~ quantile(., .75)

to me it seems an elegant syntax, i dont see much fat there to optimise away...

1 Like

no this is fantasic, just what I am looking for.

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