Akash Rastogi
Akash Rastogi

Reputation: 3

Unable to pass variable arguments to dplyr using a function in R

I am unable to pass variable arguments to dplyr using a function. A mock version of the function is

Fun.function(data, var.1, var.2) {
data %>% 
group_by_(var.1) %>%
summarise_(mean.var.2 = mean(var.2))
}

The error is

Error: Index out of bounds

It will be great if someone can help me with this.

Upvotes: 0

Views: 125

Answers (1)

phiver
phiver

Reputation: 23608

If you read through the NSE vignette you can see that you would need lazy evaluation of the functions you use inside your function. Using my example below you need to put the variables in quotes.

Looking at your function you are trying to do the following, using mtcars as an example:

  mtcars %>%
        group_by_(~ cyl) %>%
        summarise_(mean.var.2 = ~mean(mpg))

To turn this into your function you need to do the following and put the variables in quotes when you call the function.

f <- function(data, var.1, var.2) {
      data %>% 
            group_by_(lazyeval::interp(~ var, var = as.name(var.1))) %>% 
            summarise_(mean.var.2 = lazyeval::interp(~mean(var), var = as.name(var.2)))
}

 f(mtcars, "cyl", "mpg")
  cyl mean.var.2
1   4   26.66364
2   6   19.74286
3   8   15.10000

To check if they are identical to just using dplyr commands

identical(
      f(mtcars, "cyl", "mpg"),
      mtcars %>%
            group_by_(~ cyl) %>%
            summarise_(mean.var.2 = ~mean(mpg))
)
[1] TRUE

Upvotes: 1

Related Questions