The problem in question would be to apply the function f to each group of a tibble. It is a simpler way to do this, but I would like to solve the problem using the group_map() function.
Data used: starwars of the dplyr package.
What I want is to get an average of the height variable for a grouped tibble considering the variables gender and species. I know the problem could be easily solved by doing:
starwars %>% group_by(gender, species) %>% summarise(mean = mean(height, na.rm = TRUE)) However, my desire is to implement summarise(mean = mean(height, na.rm = TRUE)) in a function and send to group_map().
I tried to create the f() function that gets the data argument which is a tibble object with the previously defined groups. The second argument of the f() function would be ... so that I could pass the variables of interest from data to f().
f <- function(dados, ...){ dados %>% summarise(mean = mean(..., na.rm = TRUE)) } starwars %>% group_by(gender, species) %>% group_map(.tbl = ., .f = ~f(dados = .x), height)
...in your example implies that you want to get means of multiple columns. Is that the case, or do you want to just pass in one column to get the mean of?