You are watching: Error: this function should not be called directly
library(dplyr)# summarise peels turn off a solitary layer of groupingby_vs_am Error in n() : This duty should no be referred to as directly
I presume you have dplyr and also plyr invited in the very same session. Dplyr is no plyr. Ddply is no a duty in the dplyr package.
Both dplyr and plyr have the attributes summarise/summarize.
Look at the outcomes of conflicts() to check out masked objects.
As pointed out by the vault answer, friend may have actually a conflict between plyr and also dplyr. You have the right to to run this command come unload the plyr package.
detach("package:plyr", unload=TRUE) climate you can proceed as expected.
library(dplyr) ...summarise(n = n())
To protect against confusions with masking functions, it is clean to usage the "package::function" specification, like instance below:
In another case, this error emerged in the complying with code.
See more: Pathfinder Caryatid Column S, Pathfinder Battles: Shattered Star
library(dplyr) # dplyr 0.5.0library(lazyeval)df % group_by_(g) %>% summarise_( n = n(), amount = interp(~sum(col, na.rm = TRUE), col = as.name(g)) )# Error in n() : This role should not be called directlyIt deserve to 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# # 1 1 1 1# 2 2 2 4# 3 3 3 9
Faced comparable issue if executing code as per pointed out blog and also then run systems in detach("package:plyr", unload=TRUE)
Blog : https://www.analyticsvidhya.com/blog/2017/09/comparative-stock-analysis/
Master_Data_AutoCorrelations% 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 to be rerun it functioned fine though obtained warning message as per listed below ,not sure whether plyr gained unloaded or not.And how is the code executed effectively ?
Warning message:‘plyr’ namespace can not be unloaded: namespace ‘plyr’ is imported through ‘reshape2’, ‘scales’, ‘broom’, ‘ggplot2’ so cannot be unloaded