Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr only returning one row when using summarize [duplicate]

Tags:

r

dplyr

I'm just getting around to giving dplyr's chain operator a try.

Using the simple example:

group_by(mtcars, cyl) %>%
summarise(mean(disp), mean(hp))

I get the result:

  # mean(disp) mean(hp)
  #1   230.7219 146.6875

For some reason dplyr isn't grouping, just summarizing the entire vector. What am I missing?

like image 566
Ben Rollert Avatar asked Nov 26 '14 19:11

Ben Rollert


1 Answers

Start a fresh session this is what I get:

library(dplyr)

mtcars %>%
    group_by(cyl) %>%
    summarise(mean(disp), mean(hp))

##   cyl mean(disp)  mean(hp)
## 1   4   105.1364  82.63636
## 2   6   183.3143 122.28571
## 3   8   353.1000 209.21429

Edit

Don't load plyr second (after dplyr) or at all. The problem is that it's using plyr::summarise not dplyr::summarise:

mtcars %>%
    group_by(cyl) %>%
    plyr::summarise(mean(disp), mean(hp))

##   mean(disp) mean(hp)
## 1   230.7219 146.6875

Edit 2

You could also just be explicit and say which package to pull sumamrise from as seen below:

mtcars %>%
    group_by(cyl) %>%
    dplyr::summarise(mean(disp), mean(hp))
like image 134
Tyler Rinker Avatar answered Nov 09 '22 21:11

Tyler Rinker