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

RFunctionPlyrDplyrConflicting Libraries

R Problem Overview


I am attempting to reproduce one of the examples in the dplyr package but am getting this error message. I am expecting to see a new column n produced with the frequency of each combination. What am I missing? I triple checked that the package is loaded.

 library(dplyr)
# summarise peels off a single layer of grouping
by_vs_am <- group_by(mtcars, vs, am)

by_vs <- summarise(by_vs_am, n = n())

> Error in n() : This function should not be called directly

R Solutions


Solution 1 - R

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 - R

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 - R

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 - R

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

Solution 5 - R

Faced similar issue while executing code as per mentioned blog and then run solution in detach("package:plyr", unload=TRUE)

Blog : https://www.analyticsvidhya.com/blog/2017/09/comparative-stock-analysis/

Master_Data_AutoCorrelations<-Master_Data_lags %>%
  gather(key = "lag", value = "lag_value", -c(Stock,Date, Close)) %>%
  mutate(lag = str_sub(lag, start = 5) %>% as.numeric) %>%
  group_by(Stock, lag) %>%
  summarize(
    cor = cor(x = Close, y = lag_value, use = "pairwise.complete.obs"),
    cutoff_upper = 2/(n())^0.5,
    cutoff_lower = -2/(n())^0.5
  )

Post running detach,when above code was rerun it worked fine though received warning message as per below ,not sure whether plyr got unloaded or not.And how is the code executed properly ?

Warning message: ‘plyr’ namespace cannot be unloaded: namespace ‘plyr’ is imported by ‘reshape2’, ‘scales’, ‘broom’, ‘ggplot2’ so cannot be unloaded

Solution 6 - R

for me the solution was detach() function I utilized that function down package

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael BellhouseView Question on Stackoverflow
Solution 1 - RmnelView Answer on Stackoverflow
Solution 2 - Ruser2487691View Answer on Stackoverflow
Solution 3 - Ruser1257894View Answer on Stackoverflow
Solution 4 - RKeikuView Answer on Stackoverflow
Solution 5 - RCuriousCoderView Answer on Stackoverflow
Solution 6 - Rcamilo lopezView Answer on Stackoverflow