I am attempting to reproduce one of the examples in the dplyr package but this error message. I am expecting to see a new column n produced with the frequency of each combination. Can someone tell me what I am missing? I triple checked that the package is loaded. Thanks for the help, as always.
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
I presume you have
dplyr
andplyr
loaded in the same session.dplyr
is notplyr
.ddply
is not a function in thedplyr
package.Both
dplyr
andplyr
have the functionssummarise
/summarize
.Look at the results of
conflicts()
to see masked objects.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.
Then you can continue as expected.
In another case, this error occurred in the following code.
It can be solved as follows.
To avoid confusions with masking functions, it is clear to use the "package::function" specification, like example below:
for me the solution was
detach()
function I utilized that function down packageFaced 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/
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