0

I'm having problem with this 'textbook' example on how to aggreate data by group:

library(dplyr)    
iris %>% group_by(Species) %>% summarise(sum(Sepal.Length))

I'm getting different results on diffent R/Rstudio sessions on the same computer.

Session A: the above code returns a one line output:

  sum(Sepal.Length)
1             876.5

Session B: same code returns the expected output:

     Species sum(Sepal.Length)
1     setosa             250.3
2 versicolor             296.8
3  virginica             329.4

In session A I had open some other dataset and libraries before. However I made sure I executed library(dplyr) imediattely before the group_by()%>%summarise()

What on earth can be happening?

EDIT: based on comments bellow, I double checked, and, yes: I'm calling library(plyr) before library(dplyr). And yes, the suggestion library(dplyr) iris %>% group_by(Species) %>% summarise(sum(Sepal.Length))

LucasMation
  • 2,408
  • 2
  • 22
  • 45
  • 4
    `dplyr::summarise()` – Rorschach Jun 15 '15 at 20:00
  • 3
    You loaded `plyr` after `dplyr` and masked `dplyr::summarize` (and ignored the warning that told you about it). If you had already loaded `dplyr`, calling `library()` again right before the command won't do anything. – Gregor Thomas Jun 15 '15 at 20:02
  • tks, but I'm not. I loaded **library(plyr)** first and **library(dplyr)** second. As I said in the question, I even reloaded **library(dplyr)** to make sure – LucasMation Jun 15 '15 at 20:07
  • And, yes: **iris %>% group_by(Species) %>% dplyr::summarise(sum(Sepal.Length))** does work. – LucasMation Jun 15 '15 at 20:12
  • 2
    If `dplyr::summarise()` works then it almost certainly is an issue of `plyr::summarise()` masking `dplyr::summarise()`. As @Gregor said, calling `library("dplyr")` again will not make a difference. What happens if you run `detach(package:plyr)` and try your code again? – Kara Woo Jun 15 '15 at 20:15
  • 1
    "reloading" does nothing. It also could be something other than `plyr`... `Hmisc` has a `summarize` with a "z", and there could be others too. You can always run `searchpaths()` to see it `dplyr` is before `plyr` or not. – Gregor Thomas Jun 15 '15 at 20:27

0 Answers0