1

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!

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
ajmasnyj
  • 13
  • 4

2 Answers2

1

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))
Nick Holt
  • 116
  • 6
  • Thanks so much, Nick! Your suggestion got me on the right path, as well as nicola's https://stackoverflow.com/a/44174523/9350837, suggestion. – ajmasnyj Feb 12 '18 at 21:05
0

Thanks so much, Nick! Your suggestion got me on the right path, as well as nicola's https://stackoverflow.com/a/44174523/9350837, suggestion.

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!

ajmasnyj
  • 13
  • 4