Search code examples
rdplyrnse

Using parameters as col names in dplyr


I am struggling to mix referencing column names using parameters and directly from the dataframe. please help me correct the second function to return the same result as the first

install.packages("dplyr", version = "0.5.0")`
library(dplyr)

df <- data.frame(year = 2010:2015, GVA = 1:6)
f <- function(df) {
  df %>%
  mutate(indexGVA = GVA/max(ifelse(year == 2010, GVA, 0)) * 100)
}
f(df)

g <- function(df, val = NULL) {
  df %>%
  mutate_(indexGVA = ~val/max(ifelse(year == 2010, val, 0)) * 100)
}
g(df, val = "GVA")

Solution

  • mutate_ is now deprecated (?mutate_), you can instead use enquo and !! from rlang to quote and unquote the arguments:

    library(rlang)
    g <- function(df, val = NULL) {
      val_quo = enquo(val)
      df %>%
        mutate(indexGVA = (!! val_quo)/max(ifelse(year == 2010, (!! val_quo), 0)) * 100)
    }
    
    g(df, val = GVA)
    

    Result:

      year GVA indexGVA
    1 2010   1      100
    2 2011   2      200
    3 2012   3      300
    4 2013   4      400
    5 2014   5      500
    6 2015   6      600