Help me complete this task using purrr

This is the type of thing I do all the time in R. I know that there's an easier way to do this using map_df.

For the life of me, I can't figure out how to do this using purrr.

I want to feed the function a vector of variable names. In this case: drat, wt, qsec.

Then, I want to add a column that I am calling "name" here: Rear Axle Ratio, Weight, Quarter Mile Time.

That way I can use the name column to facet when I get to ggplot.


calc <- function(df, var, type){  
  
  df %>% 
    group_by(cyl) %>% 
    summarise(mean = mean({{var}})) %>% 
    mutate(name = type)
  
}

yyy1 <- mtcars %>% calc(drat, "Rear Axle Ratio")
yyy2 <- mtcars %>% calc(wt, "Weight")
yyy3 <- mtcars %>% calc(qsec, "Quarter Mile Time")

graph <- bind_rows(yyy1, yyy2, yyy3)

graph

I want to do something like:


calc <- function(df, var, type){  
  
  df %>% 
    group_by(cyl) %>% 
    summarise(mean = mean({{var}})) %>% 
    mutate(name = type)
  
}

vars <- c(drat, wt, qsec)
names <- c("Rear Axle Ratio", "Weight", "Quarter Mile Time")

all <- map_df(vars, names, calc)

But I can't figure out how to pass a column name into the function.

I'm not sure I understood your goal correctly. Is this close?

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
calc <- function(var, type, df = mtcars){  
  
  df %>% 
    group_by(cyl) %>% 
    summarise(mean = mean({{var}})) %>% 
    mutate(name = type)
  
}

vars <- c(as.name('drat'), as.name('wt'), as.name('qsec'))
names <- c("Rear Axle Ratio", "Weight", "Quarter Mile Time")

allDat <- map2_df(vars, names, calc)
allDat
#> # A tibble: 9 × 3
#>     cyl  mean name             
#>   <dbl> <dbl> <chr>            
#> 1     4  4.07 Rear Axle Ratio  
#> 2     6  3.59 Rear Axle Ratio  
#> 3     8  3.23 Rear Axle Ratio  
#> 4     4  2.29 Weight           
#> 5     6  3.12 Weight           
#> 6     8  4.00 Weight           
#> 7     4 19.1  Quarter Mile Time
#> 8     6 18.0  Quarter Mile Time
#> 9     8 16.8  Quarter Mile Time

Created on 2023-04-02 with reprex v2.0.2

That's exactly what I was looking for.

I didn't even know about the as.name option.

Thanks so much!

Just to simplify this a bit more.

This line:

vars <- c(as.name('drat'), as.name('wt'), as.name('qsec'))

Can be this line:

vars <- syms(c("drat","wt","qsec"))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.