如何通过组度量的方式在df中进行dplyr :: arrangeing组? [英] How to dplyr::arrange groups within a df by the mean of group measure?

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

问题描述

基于Kara Woo的

很高兴听到您的想法!

解决方案

我将尝试创建一个变量,该变量指定CredentialQ组的visCredPrcnt变量的平均值,然后像这样将其传递给安排调用:

  credData<-ReShapeAdCredSubset%>%group_by(Cr​​edentialQ,year)%&%;%summarise(vizCredPrcnt =(sum(credential_wIndiv,na.rm = TRUE)/(sum(credential_wAll,na.rm = TRUE))))%>%ungroup()%&%;%group_by(Cr​​edentialQ)%&%;%summary(meanVizCredPrcnt =平均值(visCredPrcnt,na.rm = T))%>%排列(CredentialQ,年,desc(meanVizCredPrcnt)) 

Building off of Kara Woo's https://stackoverflow.com/a/26555424/9350837 answer, I'm looking to sort my grouped df by the mean of respective groups summarized measure, vizCredPrcnt.

This is my code, thus far,

credData <- ReShapeAdCredSubset %>%
group_by(CredentialQ, year) %>%
summarise(vizCredPrcnt = (sum(credential_wIndiv, na.rm = TRUE) / (sum(credential_wAll, na.rm = TRUE)))) %>%
arrange(CredentialQ, year, desc(mean(vizCredPrcnt)))

This is the error I get,

Error in arrange_impl(.data, dots) : incorrect size (1) at position 3, expecting : 144

This is my tibble, and a visual of the grouped mean I'm looking to sort by,

grouped tibble to arrange

Happy to hear your thoughts!

解决方案

I would try creating a variable that specifies the mean of the visCredPrcnt variable by CredentialQ group, and then pass that in the arrange call like so:

credData <- ReShapeAdCredSubset %>%
    group_by(CredentialQ, year) %>%
    summarise(vizCredPrcnt = (sum(credential_wIndiv, na.rm = TRUE) / (sum(credential_wAll, na.rm = TRUE)))) %>%
    ungroup() %>%
    group_by(CredentialQ) %>%
    summarize(meanVizCredPrcnt = mean(visCredPrcnt, na.rm = T)) %>%
    arrange(CredentialQ, year, desc(meanVizCredPrcnt))

这篇关于如何通过组度量的方式在df中进行dplyr :: arrangeing组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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