R, dplyr - combination of group_by() and arrange() does not produce expected result?
dplyr, r
Solution
I think you want
ToothGrowth %>%
arrange(supp,len)
The chaining system just replaces nested commands, so first you are grouping, then ordering that grouped result, which breaks the original ordering.
Problem
when using dplyr function `group_by()` and immediately afterwards `arrange()`, I would expect to get an output where data frame is ordered within groups that I stated in `group_by()`. My reading of documentation is that this combination should produce such a result, however when I tried it this is not what I get, and googling did not indicate that other people ran into the same issue. Am I wrong in expecting this result? Here is an example, using the R built-in dataset ToothGrowth: ``` library(dplyr) ToothGrowth %>% group_by(supp) %>% arrange(len) ``` Running this will produce a data frame where the whole data frame is ordered according to `len` and not within `supp` factors. This is the code that produces the desired output: ``` ToothGrowth %>% group_by(supp) %>% do( data.frame(with(data=., .[order(len),] )) ) ```