0

How can I use the group_by() function within summarize() as below:

summarize(group_by(product),sum(Sales))
smci
  • 32,567
  • 20
  • 113
  • 146
  • But with the following codes I am getting error data%>%filter(weekdays(date)=Sunday)%>%summarize(group_by(destination),n()) Error in eval(substitute(expr), envir, enclos) : no applicable method for 'group_by_' applied to an object of class "factor" – KAFEEL BASHA Nov 08 '16 at 05:51
  • Yes I can able to get the answer with above logic, why can't I use as below summarize(group_by(de‌​stination),n()) – KAFEEL BASHA Nov 08 '16 at 06:23
  • 1
    `d %>% summarize(group_by(de‌​stination), n())` means `summarize(d, group_by(de‌​stination), n())`, which is different from `summarize(group_by(d, de‌​stination), n())`. – pe-perry Nov 08 '16 at 07:32
  • @cuttlefish isn't `iris %>% group_by(Species) %>% summarize(n = n())` doing exactly the same. It is better to use `%>%` throughout, skipping the superfluous use of `do` and making full use of the clarity piping (`%.%`) provides. – Paul Hiemstra Nov 08 '16 at 07:44
  • @Paul Hiemstra; Thanks for your advice ! – cuttlefish44 Nov 08 '16 at 07:47
  • 1
    @cuttlefish44 no problem. I really like the clean syntax of dplyr and pipes :). – Paul Hiemstra Nov 08 '16 at 07:48

1 Answers1

1

Using pipes is the easiest way, but if you need to find the issue with the current nested form, you need to pass the dataframe of interest (let's say it is df) to group_by.

Instead of:

summarize(group_by(product),sum(Sales))

use:

summarize(group_by(df,product),sum(Sales))

The first is something like:

summarize(group_by(Species), mean(Petal.Length)) 

where iris is missing.

OmaymaS
  • 1,671
  • 1
  • 14
  • 18