nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
339 stars 15 forks source link

group_by() + mutate() does not preserve row order #14

Closed nathaneastwood closed 4 years ago

nathaneastwood commented 4 years ago

For example

r$> data <- data.frame(a = c(0, 1, 0, 1, 0, 1), b = rnorm(6))                                                                                            

r$> data                                                                                                                                                 
  a          b
1 0  0.1975196
2 1 -0.9084617
3 0 -1.1766728
4 1 -2.3127030
5 0 -0.7029450
6 1 -2.2935241

r$> data %>% group_by(a) %>% mutate(b2 = b * 2)                                                                                                          
  a          b         b2
1 0  0.1975196  0.3950392
3 0 -1.1766728 -2.3533457
5 0 -0.7029450 -1.4058900
2 1 -0.9084617 -1.8169233
4 1 -2.3127030 -4.6254060
6 1 -2.2935241 -4.5870483

Groups:  a