dplyr安排不按团体安排

ZRo*_*oss 3 r dplyr

在这个超级简单的代码中,我希望dplyr首先按我的分组变量(金发)排列,然后按年龄排列,但似乎根本没有考虑到分组变量。我敢肯定,过去这对我来说是不同的。我想要(并期望)的是它会首先按金发排列(因为这是分组变量),然后按年龄排列。我正在使用dplyr_0.5.0。当我在分组变量上使用mutate进行测试时,其行为符合预期,计算了分组平均值。

我知道我可以按金发和年龄进行排列,但是我认为以前的dplyr版本会在使用排列时解释分组变量吗?我记错了吗?

# In this code I expect it to order by the grouping
# variable first (blonde) then age.
df <- data.frame(blonde = c(0,1,0,1), 
                 age=24:21)

group_by(df, blonde) %>% arrange(age)

Source: local data frame [4 x 2]
Groups: blonde [2]

  blonde   age
   <dbl> <int>
1      1    21
2      0    22
3      1    23
4      0    24
Run Code Online (Sandbox Code Playgroud)

SCD*_*DCE 5

您可以强制它使用该组:

df <- data.frame(blonde = c(0,1,0,1), 
                 age=24:21)

group_by(df, blonde) %>% arrange(age, .by_group = TRUE)

# A tibble: 4 x 2
# Groups:   blonde [2]
  blonde   age
   <dbl> <int>
1      0    22
2      0    24
3      1    21
4      1    23
Run Code Online (Sandbox Code Playgroud)

  • group_by(df,金发)%&gt;%安排(年龄,.by_group = TRUE)%&gt;%安排(desc(金发)) (2认同)