When I use group_by and summarise. my result got mix up?

Viewed 30

When I use function below, the result of summarise are not group up instead it mix together. How should I fix it?

dep %>%
 group_by(ORIGIN) %>%
  summarise(ontime_dep = sum(dep$DEP_TYPE == "ontime", na.rm = T))
2 Answers

Try this solution and let me please know.

dep %>%
 dplyr::group_by(ORIGIN) %>%
  dplyr::summarise(ontime_dep = sum(dep$DEP_TYPE == "ontime", na.rm = T))

From the code you provided it seems like you want to count the number of departures that were on time, for every origin?

In this case you could simply use count()

library(dplyr)

mtcars %>%
    filter(gear == 3) %>% 
    group_by(cyl) %>%
    count()
Related