Search code examples
rgroup-bydplyrquosure

R: Error in new_quosures(NextMethod()) : could not find function "new_quosures"


Consider a data frame:

data = data.frame(a=c(1,1,1,2,2,3),
              b=c("apples", "oranges", "apples", "apples", "apples", "grapefruit"),
              c=c(12, 22, 22, 45, 67, 28), 
              d=c("Monday", "Monday", "Monday", "Tuesday", "Wednesday", "Tuesday"),
              out = c(12, 14, 16, 18, 20, 22),
              rate = c(0.01, 0.02, 0.03, 0.04, 0.07, 0.06))

I am trying to group_by and summarize however, I keep getting the error

Error in new_quosures(NextMethod()) : 
  could not find function "new_quosures"

The code I am using is as follows:

model.data.dim.names <-  c("a", "b", "c")

data2 <- data %>% group_by_(.dots = model.data.dim.names) %>% summarise(
    mean_adj1 = (mean(out, na.rm=FALSE)),
    mean_adj2 = (mean(out)/mean(rate))
  )

Please note, this is dummy data and the error is reproduced in the windows OS with the dummy data. Additionally, I am working on Windows OS. Additionally, I have tried the following:

  1. Remove plyr
  2. Check and edit the NA/ Infinite values
  3. Turning data frame to data table and running the code

Could you please help me understand the root cause of the error or an alternative that I could use?

Answer: 

1) tidyr library screws up with it. Removing tidyr helps
2) use most updated dplyr library and group_by/ group_by_at/group_by(!!!syms(model.data.dim.names) works

Solution

  • The group-by_ function is deprecated, the current tidyeval way to do this would be to convert the character vector to symbols and then unquote splice them into group_by:

    library(dplyr)
    
    data %>%
      group_by(!!!syms(model.data.dim.names)) %>% 
      summarise(
        mean_adj1 = mean(out, na.rm=FALSE),
        mean_adj2 = mean(out) / mean(rate)
      )
    ## A tibble: 6 x 5
    ## Groups:   a, b [4]
    #      a b              c mean_adj1 mean_adj2
    #  <dbl> <fct>      <dbl>     <dbl>     <dbl>
    #1     1 apples        12        12     1200 
    #2     1 apples        22        16      533.
    #3     1 oranges       22        14      700 
    #4     2 apples        45        18      450 
    #5     2 apples        67        20      286.
    #6     3 grapefruit    28        22      367.