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

Chris Evans chr|@ho|d @end|ng |rom p@yctc@org
Sun Jul 5 10:43:35 CEST 2020


Apologies if this is a stupid question but searching keeps getting things I know and don't need. 

What I want to do is to use the group-by() power of dplyr to run functions that expect a dataframe/tibble per group but I can't see how do it. Here is a reproducible example. 

### create trivial tibble
n <- 50 
x <- 1:n 
y <- sample(1:3, n, replace = TRUE) 
z <- rnorm(n) 
tib <- as_tibble(cbind(x,y,z)) 

### create trivial function that expects a tibble/data frame
sillyFun <- function(tib){ 
return(list(nrow = nrow(tib), 
ncol = ncol(tib))) 
} 

### works fine on the whole tibble
tib %>% 
summarise(dim = list(sillyFun(.))) %>% 
unnest_wider(dim) 

That gives me:
# A tibble: 1 x 2
   nrow  ncol
  <int> <int>
1    50     3


### So I try the following hoping to apply the function to the grouped tibble
tib %>% 
group_by(y) %>% 
summarise(dim = list(sillyFun(.))) %>% 
unnest_wider(dim)

### But that gives me:
# A tibble: 3 x 3
      y  nrow  ncol
  <dbl> <int> <int>
1     1    50     3
2     2    50     3
3     3    50     3

Clearly "." is still passing the whole tibble, not the grouped subsets.  What I can't find is whether there is an alternative to "." that would pass just the grouped subset of the tibble.

I have bodged my way around this by writing a function that takes individual columns and reassembles them into a data frame that the actual functions I need to use require but that takes me back to a lot of clumsiness both selecting the variables to pass in the dplyr call to the function and putting the reassemble-to-data-frame bit in the function I call.  (The functions I really need are reliability explorations and can called on whole dataframes.)

I know I can do this using base R split and lapply but I feel sure it must be possible to do this within dplyr/tidyverse.  I'm slowly transferring most of my code to the tidyverse and hitting frustrations but also finding that it does really help me program more sensibly, handle relational data structures more easily, and write code that I seem better at reading when I come back to it after months on other things so I am slowly trying to move all my coding to tidyverse.  If I could see how to do this, it would help.

Very sorry if the answer should be blindingly obvious to me.  I'd also love to have pointers to guidance to the tidyverse written for people who aren't professional coders or statisticians and that go a bit beyond the obvious basics of tidyverse into issues like this.

TIA,

Chris

-- 
Small contribution in our coronavirus rigours: 
https://www.coresystemtrust.org.uk/home/free-options-to-replace-paper-core-forms-during-the-coronavirus-pandemic/

Chris Evans <chris using psyctc.org> Visiting Professor, University of Sheffield <chris.evans using sheffield.ac.uk>
I do some consultation work for the University of Roehampton <chris.evans using roehampton.ac.uk> and other places
but <chris using psyctc.org> remains my main Email address.  I have a work web site at:
   https://www.psyctc.org/psyctc/
and a site I manage for CORE and CORE system trust at:
   http://www.coresystemtrust.org.uk/
I have "semigrated" to France, see: 
   https://www.psyctc.org/pelerinage2016/semigrating-to-france/ 
   https://www.psyctc.org/pelerinage2016/register-to-get-updates-from-pelerinage2016/

If you want an Emeeting, I am trying to keep them to Thursdays and my diary is at:
   https://www.psyctc.org/pelerinage2016/ceworkdiary/
Beware: French time, generally an hour ahead of UK.



More information about the R-help mailing list