0
votes

I would like to pass a column name as an argument to a function I've created that uses the dplyr package.

df = data.frame(grade = c(1,1,1,3,3,5,7,8,8,4),
                score = c(10,20,40,43,56,29,59,37,61,88))

tmp.func = function(df.name, variable.name, year.label){

  require("dplyr")

  df = df.name %>%
    group_by(grade) %>%
    summarise(n = n(),
              M = mean(variable.name),
              SD = sd(variable.name),
              P25 = quantile(variable.name, probs = .25),
              P50 = quantile(variable.name, probs = .50),
              P75 = quantile(variable.name, probs = .75)) %>%
    mutate(grade = as.numeric(as.character(variable.name))) %>%
    arrange(grade) %>%
    dplyr::select(grade,
                  n,
                  M, 
                  SD,
                  P25, 
                  P50, 
                  P75)

  colnames(df) = paste(names(df), ".", year.label, sep = "")

  df

}

tmp = tmp.func(df.name = df, variable.name = "score", year.label = ".1718")

This code results in the below error message. I have to run this same function dozens of times so I need to create a function that can handle this. Is there a better way to approach the problem?

Error in (1 - h) * qs[i] : non-numeric argument to binary operator
In addition: There were 12 warnings (use warnings() to see them)
1
I think the problem here is the difference between the tidyverse's nonstandard evaluation and you passing a string in to the function. Check this out - svenhalvorson

1 Answers

0
votes

You can do that with use of enquo and !! to "unquot" the expression again. For more information see this section Different input variable as @svenhalvorson suggested.

my_summarise2 <- function(df, expr) {
  expr <- enquo(expr)

  summarise(df,
    mean = mean(!! expr),
    sum = sum(!! expr),
    n = n()
  )
}

usage

my_summarise2(df, score)