简体   繁体   中英

Data frame column as argument to dplyr R function

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM