Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you obtain the group_by value for use in passing to a function?

Tags:

r

dplyr

I am trying to use dplyr to apply a function to a data frame that is grouped using the group_by function. I am applying a function to each row of the grouped data using do(). I would like to obtain the value of the group_by variable so that I might use it in a function call.

So, effectively, I have-

tmp <-
  my_data %>%
  group_by(my_grouping_variable) %>%
  do(my_function_call(data.frame(x = .$X, y = .$Y),
                      GROUP_BY_VARIABLE)

I'm sure that I could call unique and get it...

do(my_function_call(data.frame(x = .$X, y = .$Y),
                    unique(.$my_grouping_variable))

But, it seems clunky and would inefficiently call unique for every grouping value.

Is there a way to get the value of the group_by variable in dplyr?

I'm going to prematurely say sorry if this is a crazy easy thing to answer. I promise that I've exhaustively searched for an answer.

like image 975
Chris Avatar asked Nov 01 '25 06:11

Chris


1 Answers

First, if necessary, check if it's a grouped data frame: inherits(data, "grouped_df").

If you want the subsets of data frames, you could nest the groups:

mtcars %>% group_by(cyl) %>% nest()

Usually, you won't nest within the pipe-chain, but check in your function:

your_function(.x) <- function(x) {
  if(inherits(x, "grouped_df")) x <- nest(x)
}

Your function should then iterate over the list-column data with all grouped subsets. If you use a function within mutate, e.g.

mtcars %>% group_by(cyl) %>% mutate(abc = your_function_call(.x))

then note that your function directly receives the values for each group, passed as class structure. It's a bit difficult to explain, just try it out and debug your_function_call step by step...

like image 60
Daniel Avatar answered Nov 03 '25 19:11

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!