1

I'm getting unexpected results when trying to sum by group using dplyr. Instead of getting the sum of sales for each person, I keep getting the sum of sales for everyone. Here's an example:

library(dplyr)

df <- data.frame(name = rep(c("John","Mary", "Lee"),3), 
                 year = rep(c(2014,2013,2012), each=3), 
                 sales = rep(c(10,20,30),3)
                )

df %>% 
  group_by(name) %>%
  summarise(totals = sum(sales))

This should be so simple but I can't seem to get it to work. What am I doing wrong?

Phoebe
  • 287
  • 4
  • 16

0 Answers0