Dplyr sums by nested group_by

I have a data frame like this:

Date Amount Category 1 02.07.15 1 1 2 02.07.15 2 1 3 02.07.15 3 1 4 02.07.15 4 2 5 03.07.15 5 2 6 04.07.15 6 3 7 05.07.15 7 3 8 06.07.15 8 3 9 07.07.15 9 4 10 08.07.15 10 5 11 09.07.15 11 6 12 10.07.15 12 4 13 11.07.15 13 4 14 12.07.15 14 5 15 13.07.15 15 5 16 14.07.15 16 6 17 15.07.15 17 6 18 16.07.15 18 5 19 17.07.15 19 4 

I would like to calculate the amount for each individual day in the category. My attempts (see Code) are not sufficient.

 summarise(group_by(testData, Category), sum(Amount)) 

Incorrect output → here the sum is calculated for each group

  Category sum(Amount) 1 1 6 2 2 9 3 3 21 4 4 53 5 5 57 6 6 44 summarise(group_by(testData, Date), sum(Amount), categories = toString(Category)) 

Wrong conclusion → here the amount is calculated for each day, but the categories are not considered

  Date sum(Amount) categories 1 02.07.15 10 1, 1, 1, 2 2 03.07.15 5 2 3 04.07.15 6 3 4 05.07.15 7 3 5 06.07.15 8 3 6 07.07.15 9 4 7 08.07.15 10 5 8 09.07.15 11 6 9 10.07.15 12 4 10 11.07.15 13 4 11 12.07.15 14 5 12 13.07.15 15 5 13 14.07.15 16 6 14 15.07.15 17 6 15 16.07.15 18 5 16 17.07.15 19 4 

So far, I have not been able to combine both statements. How can I insert both group_by statements to calculate the sum of the amount for each individual day in each category?

Attachment of groups such as:

summary (group_by (group_by (testData, Date), Category), amount (amount), dates = toString (date))

  Category sum(Amount) dates 1 1 6 02.07.15, 02.07.15, 02.07.15 2 2 9 02.07.15, 03.07.15 3 3 21 04.07.15, 05.07.15, 06.07.15 4 4 53 07.07.15, 10.07.15, 11.07.15, 17.07.15 5 5 57 08.07.15, 12.07.15, 13.07.15, 16.07.15 6 6 44 09.07.15, 14.07.15, 15.07.15 

Does not work properly.

I heard about dplyr - summed the weighted summarise_each data , but couldn't get it to work:

 summarise_each(testData, funs(Category)) Error could not find function Category 
+5
source share
1 answer

You can try

  testData %>% group_by(Date,Category) %>% summarise(Amount= sum(Amount)) 
+5
source

All Articles