dplyr: "Error in n(): function should not be called directly"

Solution 1:

I presume you have dplyr and plyr loaded in the same session. dplyr is not plyr. ddply is not a function in the dplyr package.

Both dplyr and plyr have the functions summarise/summarize.

Look at the results of conflicts() to see masked objects.

Solution 2:

As mentioned by the previous answer, you may have a conflict between plyr and dplyr. You can to run this command to unload the plyr package.

detach("package:plyr", unload=TRUE) 

Then you can continue as expected.

library(dplyr) 
...
summarise(n = n()) 

Solution 3:

To avoid confusions with masking functions, it is clear to use the "package::function" specification, like example below:

delay <- dplyr::summarise(by_tailnum, 
  count = n(), 
  dist = mean(distance, na.rm = TRUE), 
  delay = mean(arr_delay, na.rm = TRUE))

Solution 4:

In another case, this error occurred in the following code.

library(dplyr) # dplyr 0.5.0
library(lazyeval)

df <- data_frame(group = c(1, 2, 2, 3, 3, 3))

g <- "group"

df %>%
  group_by_(g) %>%
  summarise_(
    n = n(),
    sum = interp(~sum(col, na.rm = TRUE), col = as.name(g))
  )
# Error in n() : This function should not be called directly

It can be solved as follows.

df %>%
  group_by_(g) %>%
  summarise_(
    n = "n()",
    sum = interp(~sum(col, na.rm = TRUE), col = as.name(g))
  )
# A tibble: 3 × 3
# group     n   sum
# <dbl> <int> <dbl>
# 1     1     1     1
# 2     2     2     4
# 3     3     3     9