[英]How to dplyr::arrange groups within a df by the mean of group measure?
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
. 在Kara Woo的https://stackoverflow.com/a/26555424/9350837答案的基础上,我希望通过各个组的摘要度量vizCredPrcnt
对我的分组df进行排序。
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 range_impl(.data,点)中的错误:位置3处的尺寸(1)不正确,预期为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: 我将尝试创建一个由CredentialQ组指定visCredPrcnt变量均值的变量,然后将其通过如下所示的安排调用传递:
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))
Thanks so much, Nick! 非常感谢,尼克! Your suggestion got me on the right path, as well as nicola's https://stackoverflow.com/a/44174523/9350837 , suggestion. 您的建议,以及尼古拉的https://stackoverflow.com/a/44174523/9350837 ,都使我走上了正确的道路。
I took both of your suggestions, and smooshed them together for this, which does the trick! 我接受了您的两个建议,并为此将它们融合在一起,可以解决问题!
credData <- ReShapeAdCredSubset %>%
group_by(CredentialQ, year) %>%
summarise(vizCredPrcnt = (sum(credential_wIndiv, na.rm = TRUE) / (sum(credential_wAll, na.rm = TRUE))))
credData <- credData %>%
mutate(meanViz = mean(vizCredPrcnt, na.rm = TRUE)) %>%
group_by(CredentialQ) %>%
arrange(desc(meanViz))
Thanks again! 再次感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.