Search code examples
rfunctiondplyrtidyeval

Pull a column given as argument in function


I have a function that takes in a dataset and a variable as argument. It needs to filter based on some criteria, pull the column with name as variable and calculate its mean then. I am not able to pass the variable as column name though. Kindly help.

MeanFor <- formula(df, flag, var){
  df2 <- df %>% filter(member == flag) %>% pull(var) %>% mean(.)
}

My df looks like this

df <- data.frame(name = c("A","B","C","D"),
member = c("Y","Y","Y","N"), sales_jan = c("100","0","130","45"), sales_feb = c("44","0","67","0"))

I can provide the flag as "Y"/"N" and want to provide "sales_jan"/"sales_feb"/.../"sales_dec" as var input.


Solution

  • You can write the function as :

    library(dplyr)
    
    MeanFor <- function(df, flag, var){
       df %>% filter(member == flag) %>% pull(.data[[var]]) %>% as.numeric() %>% mean
    }
    
    df %>% MeanFor('Y', 'sales_jan')
    #[1] 76.66667
    
    df %>% MeanFor('Y', 'sales_feb')
    #[1] 37
    

    The function can be written in base R as :

    MeanFor <- function(df, flag, var){
        mean(as.numeric(subset(df, member == flag)[[var]]))
    }