0 votes
1 view
in R Programming by (5.1k points)
recategorized by

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

1 Answer

0 votes
by (24.7k points)

You can try the following to remove this error:

  • Make sure you load plyr first

  • Both dplyr and plyr have the functions summarise/summarize. Look at the results of conflicts() to see masked objects.
  • Use summarize with the package, something like this 

dplyr::summarize(count = n())

  •  You can run this command to unload the plyr package.

detach("package:plyr", unload=TRUE) 

Then you can continue as expected.

library(dplyr) 

...

summarise(n = n()) 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...