I have a data set that tracks daily revenue by id, category and date:
id cat date daily_rev 111 A 3/09/19 $10 111 A 3/10/19 $15 111 A 3/11/19 $40 222 A 3/09/19 $100 222 A 3/10/19 $150 222 A 3/11/19 $50 333 B 3/09/19 $45 333 B 3/10/19 $10 333 B 3/11/19 $30
I want to manipulate the data to sum across all dates by category:
cat tot_daily_rev A $365 B $85
When I use this code:
X <- data %>%
group_by(cat) %>%
mutate(tot_daily_rev = sum(daily_rev))
I get a data frame that has a tot_daily_rev column that is a sum of every row in the data set:
id cat date daily_rev tot_daily_rev 111 A 3/09/19 $10 $450 111 A 3/10/19 $15 $450 111 A 3/11/19 $40 $450 222 A 3/09/19 $100 $450 222 A 3/10/19 $150 $450 222 A 3/11/19 $50 $450 333 B 3/09/19 $45 $450 333 B 3/10/19 $10 $450 333 B 3/11/19 $30 $450
I've already referenced this post: How to sum a variable by group?, but it does not solve my issue.
--
Update
Why does summarize or mutate not work with group_by when I load `plyr` after `dplyr`? addresses the same issue! I was completely unaware that this was an issue of functions/libraries, so I didn't think to search for why summarize and mutate were not behaving as I expected.