Ram
Ram

Reputation: 1

Referring to variables by name in a dplyr function returns object not found error

library(nycflights13)
library(dplyr)
sum.na <- function(df,x){df %>% summarise(n=sum(is.na(x)))}
sum.na(flights, arr_time)

When I run the above code, I get the error below:

  **Error in eval(cols[[col]], .data, parent.frame()) : 
  object 'arr_time' not found**

Upvotes: 0

Views: 582

Answers (2)

akrun
akrun

Reputation: 887891

We can also use ensym with !!

library(nycflights13)
library(dplyr)
sum.na <- function(df, x) { 
                 df %>%
                   summarise(n = sum(is.na(!! ensym(x))))
   }

 

It can take both quoted as well as unquoted

 sum.na(flights, arr_time)
 # A tibble: 1 x 1
 #     n
 # <int>
 #1  8713

 sum.na(flights, 'arr_time')
# A tibble: 1 x 1
#      n
#  <int>
#1  8713

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389265

Use curly-curly ({{}}) to pass column names as function argument.

library(nycflights13)
library(dplyr)

sum.na <- function(df,x){df %>% summarise(n=sum(is.na({{x}})))}

sum.na(flights, arr_time)
# A tibble: 1 x 1
#      n
#  <int>
#1  8713

Upvotes: 1

Related Questions