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.