Dplyr returns only one row when using summation

I just get around to asking the dplyr chain operator.

Using a 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 is not grouped by simply adding up the entire vector. What am I missing?

+5
source share
1 answer

Start a new session, this is what I get:

 library(dplyr) group_by(mtcars, 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

Do not load plyr second (after dplyr ) or at all. The problem is that it uses plyr::summarise not dplyr::summarise :

 group_by(mtcars, cyl) %>% plyr::summarise(mean(disp), mean(hp)) ## mean(disp) mean(hp) ## 1 230.7219 146.6875 
+14
source

Source: https://habr.com/ru/post/1207760/


All Articles