如何在已排序的组中对组进行排序? [英] How to sort groups within sorted groups?
问题描述
我无法解决dplyr功能的其他一些复杂问题.主要是,我想对已经排序的组中的第二个组进行排序.
There is this extra bit of complications on dplyr functionality that I haven't been able to solve. Mainly, I want to sort a second group within an already sorted group.
所以我有这个data.frame:
So I have this data.frame:
a_table <- data.frame(id=1:30,
grp1 = sample(LETTERS[1:5], 30, replace=TRUE, prob=c(1,1,2,2,3)),
grp2 = sample(letters[6:8], 30, replace=TRUE, prob=c(2,2,3)))
我首先用grp1
分组,对条目进行计数并对其进行排序,然后对于每个grp1
,我对每个grp2
的值进行计数并对其进行排序.
I first group by grp1
count the entries and order them, then for each grp1
I count the values of each grp2
and order them.
我尝试这样做:
a_summary <- a_table %>%
group_by(grp1) %>%
mutate(frst_count = n()) %>%
arrange(desc(frst_count)) %>%
group_by(grp2) %>%
mutate(scnd_count = n()) %>%
arrange(desc(scnd_count))
但是显然缺少一些东西,因为没有组summarise
,因此也没有组排序.其他使用summarise
的尝试没有区分组1和2.
But there's obviously something missing because there's no group summarise
and therefore no group sorting. Other tries with summarise
haven't distinguished the group 1 and 2.
谢谢.
推荐答案
默认情况下,group_by
具有add = FALSE
,这意味着您不添加第二级分组,而是覆盖了第一级分组,从而导致错误.
By default, group_by
has add = FALSE
, which means rather than adding the second level of grouping, you are overwriting the first, leading to your error.
您可以使用:
library(dplyr)
a_table %>% group_by(grp1) %>%
mutate(frst_count = n()) %>%
group_by(grp2, add = TRUE) %>%
mutate(scnd_count = n()) %>%
arrange(frst_count, scnd_count)
这篇关于如何在已排序的组中对组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!