0

I'm trying to use the dplyr function summarise together with group by to get the sum of my factors. This is my dataset:

brand = c("Mercedes","Mercedes","Mercedes","Mercedes","Mercedes",
          "Mercedes","Mercedes","Mercedes","Mercedes","BMW",
          "BMW","BMW","BMW","BMW","BMW","BMW","BMW","BMW")
model = c("SL_class", "SL_class", "SL_class", "A_class", 
          "A_class", "A_class", "E_class", "E_class", 
          "E_class", "4 Series", "4 Series", "4 Series", 
          "X1", "X1", "X1", "Z4", "Z4", "Z4")
variable = c(5,6,7,12,13,14,1,2,3,7,8,9,22,24,25,11,12,14)


data = data.frame(brand, model, variable)
data

data$brand <- factor(data$brand)
data$model <- factor(data$model)

But if I try to get the sum of variables for each level of brand I do get only a value (see below). Why do I lose the grouping?

data %>% 
  dplyr::group_by(brand) %>% 
  summarise(SUM = sum(variable))

  SUM
1 195

This is instead the result that I would like to get:

aggregate(data$variable, by = list(data$brand), 
          FUN =sum)

Group.1   x
1      BMW 132
2 Mercedes  63

Can someone help me to get this working in dplyr?

Valen78
  • 115
  • 6
  • 1
    What if you use `dplyr::summarise()`. Do you have other packages loaded that also define a `summarise()` function? I can't replicate your problem. I get two rows when I copy/paste your code. Did you call `library(dplyr)` to load the library? – MrFlick Sep 29 '21 at 21:13
  • 1
    Maybe: [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 Sep 29 '21 at 21:15
  • It works if you use `library(dplyr)` instead of `dplyr::group_by(brand)` – ViviG Sep 29 '21 at 21:31
  • Thank you for you help. I've solved it. It was the package Rmisc that was loading plyr after dplyr. If I load the package plyr first, it works – Valen78 Sep 30 '21 at 05:26

0 Answers0