More on Column Names as Parameters

Use quo or enquo when you want to include column names as parameters in a function. For example, a function like the following would not work:

bad_function <- function(data, col_name){
  
  newdf <- data %>%
    mutate('adjusted_column' = col_name + 1)
  
  return(newdf)
  
}

bad_function(df, column_i_care_about)

because column_i_care_about isn’t specified in a form that mutate can work with.

Examples

The data are contained in df1.

df1 <- data.frame(
  a = c(1,2,NA),
  b = c(NA,3,4)
)

df1
   a  b
1  1 NA
2  2  3
3 NA  4

The function: take the column specified by the parameter and add one to every value. Then return the new data frame.

adder <- function(col_use){
  
  newdf <- df1 %>%
    mutate('adder' = 
             (!!col_use) + 1)  # correct form here using !!
    
  return(newdf)
  
}

adder(quo(a))                 # correct form here using quo
   a  b adder
1  1 NA     2
2  2  3     3
3 NA  4    NA

A more complicated function by incorporating is.na.

na_tagger <- function(col_use){
  
  newdf <- df1 %>%
    mutate('na_tag' = 
             ifelse(is.na((!!col_use)) == T, 1, 0))
  
  return(newdf)
}

na_tagger(quo(a))
   a  b na_tag
1  1 NA      0
2  2  3      0
3 NA  4      1

In the examples above I used quo interactively. You get the same result by instead using enquo within the function.

adder2 <- function(col_use){
  
  col_use <- enquo(col_use)
  
  newdf <- df1 %>%
    mutate('adder' = 
             (!!col_use) + 1)
  
  return(newdf)
}

adder2(a)
   a  b adder
1  1 NA     2
2  2  3     3
3 NA  4    NA

One More Note

Sometimes I also need to specify the data set and column within a dplyr command and then use the parameter to select a specific row. The following format seems to work well: data[['col_name']][row]. Here is a function that is inefficient but demonstrates the point well:

selector2 <- function(x, y){
  
  new <- df1 %>%
    filter(robby == df1[['robby']][x]) %>%
    filter(ruddy == df1[['ruddy']][y])
  
  return(new)
}

Bo\(^2\)m =)