dplyr如何对已排序组中的组进行排序? [英] dplyr How to sort groups within sorted groups?

查看:151
本文介绍了dplyr如何对已排序组中的组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dplyr功能有一些额外的复杂性,我一直无法解决。主要是,我想排序已经排序的组中的第二个组。



所以我有这个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(字母[6:8],30,replace = TRUE,prob = c(2,2,3)))

第一组由 grp1 进行计数并对它们进行排序,然后对每个 grp1 我计算每个 grp2 的值并对它们进行排序。



我尝试这样做:

  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))

但是显然有一些缺失,因为没有组 summary> ,因此没有组排序。其他尝试使用 summarize 并未区分组1和2.



谢谢。

解决方案

默认情况下, group_by = FALSE ,这意味着不是添加第二级别的分组,您将覆盖第一级,导致您的错误。



您可以使用:

  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)


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.

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))) 

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.

My attempt to do this:

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))

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.

Thanks.

解决方案

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.

You could use:

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)

这篇关于dplyr如何对已排序组中的组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆