如何通过group measure在一个df中dplyr::arrange分组?
How to dplyr::arrange groups within a df by the mean of group measure?
基于 Kara Woo 的 回答,我希望根据各个组的汇总度量对我的分组 df 进行排序,vizCredPrcnt
。
到目前为止,这是我的代码,
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)))
这是我得到的错误,
Error in arrange_impl(.data, dots) :
incorrect size (1) at position 3, expecting : 144
这是我的小标题,我正在寻找排序依据的分组平均值的视觉效果,
分组排列
很高兴听到您的想法!
我会尝试创建一个变量来指定 CredentialQ 组的 visCredPrcnt 变量的平均值,然后像这样在 arrange 调用中传递它:
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))
非常感谢,尼克!您的建议以及尼古拉 的建议让我走上了正确的道路。
我采纳了你的两个建议,并为此将它们融合在一起,这很有效!
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))
再次感谢!
基于 Kara Woo 的 回答,我希望根据各个组的汇总度量对我的分组 df 进行排序,vizCredPrcnt
。
到目前为止,这是我的代码,
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)))
这是我得到的错误,
Error in arrange_impl(.data, dots) : incorrect size (1) at position 3, expecting : 144
这是我的小标题,我正在寻找排序依据的分组平均值的视觉效果,
分组排列
很高兴听到您的想法!
我会尝试创建一个变量来指定 CredentialQ 组的 visCredPrcnt 变量的平均值,然后像这样在 arrange 调用中传递它:
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))
非常感谢,尼克!您的建议以及尼古拉
我采纳了你的两个建议,并为此将它们融合在一起,这很有效!
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))
再次感谢!