Search code examples
rsortingdataframedplyrgroup-summaries

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.

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

enter image description here

Happy to hear your thoughts!


Solution

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