Michael Bellhouse
Michael Bellhouse

Reputation: 1577

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

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

Upvotes: 101

Views: 67917

Answers (6)

camilo lopez
camilo lopez

Reputation: 1

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

Upvotes: 0

CuriousCoder
CuriousCoder

Reputation: 41

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

Upvotes: 0

Keiku
Keiku

Reputation: 8823

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

Upvotes: 8

user1257894
user1257894

Reputation: 671

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

Upvotes: 26

user2487691
user2487691

Reputation: 391

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()) 

Upvotes: 39

mnel
mnel

Reputation: 115485

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.

Upvotes: 120

Related Questions