Vaibhav Singh
Vaibhav Singh

Reputation: 1209

Pass arugments to R custom function using map (purrr)

Using iris for reproducibility

library(tidyverse)

mean_by <- function(data,by,var1) {
    data %>% 
    group_by({{by}}) %>% 
    summarise(avg=mean({{var1}}))
}


iris %>% mean_by(Species,Petal.Width) # This works

map_dfr(iris,mean_by,Species) # This doesnt work, I want to run this to all numerical columns in iris, how do I do that.

More importantly, fundamental question: how do I pass arguments to custom function using map_dfr in R

Upvotes: 2

Views: 692

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

Ideally you should use across :

library(dplyr)
library(purrr)

iris %>%
  group_by(Species) %>%
  summarise(across(where(is.numeric), mean, na.rm = TRUE))

#  Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
#  <fct>             <dbl>       <dbl>        <dbl>       <dbl>
#1 setosa             5.01        3.43         1.46       0.246
#2 versicolor         5.94        2.77         4.26       1.33 
#3 virginica          6.59        2.97         5.55       2.03 

To use map_dfr for numeric variables we can change the function to :

mean_by <- function(data,by,var1) {
  data %>% 
    group_by({{by}}) %>% 
    summarise(avg = mean(.data[[var1]]))
}

map_dfr(names(select(iris, where(is.numeric))), 
              mean_by, data = iris, by = Species)

#  Species      avg
#   <fct>      <dbl>
# 1 setosa     5.01 
# 2 versicolor 5.94 
# 3 virginica  6.59 
# 4 setosa     3.43 
# 5 versicolor 2.77 
# 6 virginica  2.97 
# 7 setosa     1.46 
# 8 versicolor 4.26 
# 9 virginica  5.55 
#10 setosa     0.246
#11 versicolor 1.33 
#12 virginica  2.03 

Upvotes: 3

Related Questions