0

Why does this piece of code:

letter = c("A","B","C","C","A")
product = c("Beef","Chicken","Beef","Beef","Beef")
value = c(10,20,40,10,5)
df <- data.frame(letter,product,value)

df1 = df %>% group_by(letter,product) %>% summarise(value = sum(value))

Results in this:

> df1
  value
1    85

Rather than this?

> df
  letter product value
1      A    Beef    15
2      B Chicken    20
3      C    Beef    50
Afke
  • 899
  • 2
  • 10
  • 21
  • 4
    [Why are my dplyr group_by & summarize not working properly? (name-collision with plyr)](https://stackoverflow.com/questions/26923862/why-are-my-dplyr-group-by-summarize-not-working-properly-name-collision-with); [Why does summarize or mutate not work with group_by when I load `plyr` after `dplyr`?](https://stackoverflow.com/questions/26106146/why-does-summarize-or-mutate-not-work-with-group-by-when-i-load-plyr-after-dp) – Henrik Oct 22 '18 at 14:10
  • 1
    Following the link provided by @Henrik, it looks like you have loaded `plyr` after `dplyr`. Here is what happened: `df %>% group_by(letter, product) %>% dplyr::summarise(value = sum(value))` and `df %>% group_by(letter, product) %>% plyr::summarise(value = sum(value))` – nghauran Oct 22 '18 at 14:15
  • Thanks everyone! This solved my problem: df = df %>% group_by(letter,product) %>% dplyr::summarise(value = sum(value)) – Afke Oct 22 '18 at 14:18
  • How should I handle this duplicate question? – Afke Oct 22 '18 at 14:26
  • @Afke I think you should just leave it as a signpost for the linked posts. Cheers – Henrik Oct 22 '18 at 14:53

0 Answers0