0

I have data where field names are not known in advance and want to write some functions to perform basic analysis and transformations. Would like to use dplyr for conformity with other apps. Have done some reading around Programming in dplyr, which explains the use of quo() and enquo(). Also there is an interesting thread at Pass arguments to dplyr functions which seems to trace the history of non-standard evaluation in dplyr.

A simple example is to find the numeric fields in a dataframe and sum them.

sum_numeric_fields <- function(df) {
 # get the name and data type of each field
   n <- names(df)
   t <- sapply(df, class) 

 # select just the numeric fields 
   nums <- n[t[] == "numeric"]

 # sum the numeric fields
  for (col in nums) {
    col<- enquo(col)
    df %>% summarise(sum = sum(!!col))
    }
}

Where it goes wrong (say running it on the iris dataset), is that it fails to get rid of the quotes on the column name, despite the !!. Have tried the curly curly idea without success too. It seems to be something to do with the behaviour of the nums vector in the loop.

Grateful for advice.

mer_curius
  • 524
  • 6
  • 12

0 Answers0