I think there are a couple reasons your map2_dfr() is giving an error. The first is that unlike many other tidyverse functions, the map family of functions generally don't take dataframes as their inputs, which is what happens when you pipe starwars as the first argument of map2_dfr().
In the case of map2(), it expects .x and .y to be vectors, and you are correctly specifying them here as your grouping and mean vars. But then you want to iterate over those vectors and use them as the arguments in your mean_grouped() function, applied to the starwars data frame.
A second issue is discussed in the last example here: https://www.tidyverse.org/articles/2019/06/rlang-0-4-0/. Because you are passing string vectors to your custom function (by way of map2()), you should refer to them using .data[[var]] rather than {{var}}.
I'm not sure exactly what the final output you're hoping for is, but below is an example that might be what you're after.
library(tidyverse)
mean_grouped <- function(data, groupvar, meanvar) {
data %>%
group_by(.data[[groupvar]]) %>%
summarise(mean = mean(.data[[meanvar]], na.rm = TRUE))
}
group_vars <- c(
"gender", "homeworld", "species",
"hair_color", "eye_color", "skin_color"
)
mean_vars <- c("height", "mass", "birth_year")
vars_list <- list(x = group_vars, y = mean_vars)
cross_list <- cross_df(vars_list)
map2_dfr(
.x = cross_list$x,
.y = cross_list$y,
.f = ~ mean_grouped(starwars, .x, .y)
)
#> # A tibble: 453 x 7
#> gender mean homeworld species hair_color eye_color skin_color
#> <chr> <dbl> <chr> <chr> <chr> <chr> <chr>
#> 1 <NA> 120 <NA> <NA> <NA> <NA> <NA>
#> 2 female 165. <NA> <NA> <NA> <NA> <NA>
#> 3 hermaphrodite 175 <NA> <NA> <NA> <NA> <NA>
#> 4 male 179. <NA> <NA> <NA> <NA> <NA>
#> 5 none 200 <NA> <NA> <NA> <NA> <NA>
#> 6 <NA> 139. <NA> <NA> <NA> <NA> <NA>
#> 7 <NA> 176. Alderaan <NA> <NA> <NA> <NA>
#> 8 <NA> 79 Aleen Minor <NA> <NA> <NA> <NA>
#> 9 <NA> 175 Bespin <NA> <NA> <NA> <NA>
#> 10 <NA> 180 Bestine IV <NA> <NA> <NA> <NA>
#> # … with 443 more rows
Created on 2019-06-30 by the reprex package (v0.3.0)