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
Michael Bellhouse
I am a market researcher deeply experienced in product testing, brand positioning and measuring in-market performance.
Updated on January 01, 2020Comments
-
Michael Bellhouse over 4 years
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