title: Ordering data in R by the sum of a factor-associated numbers
body: <p>Thanks in advance for the help. I'm trying to order data by levels of one factor, and then by the sum of numeric data associated with another factor. Secondly, I'm trying to apply group numbers. I want to do this without manual input, such that a script can be saved for different, future data. To clarify, I've created an example dataset:</p>
  <pre><code>library(dplyr)
  
  Year &lt;- c(&quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2020&quot;,
  &quot;2020&quot;, &quot;2020&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2019&quot;, &quot;2019&quot;, &quot;2019&quot;)
  Model &lt;- factor(c(&quot;Mustang&quot;, &quot;Mustang&quot;, &quot;Mustang&quot;, &quot;F150&quot;, &quot;F150&quot;, &quot;F150&quot;, &quot;F150&quot;, &quot;F150&quot;,
  &quot;F150&quot;, &quot;Cruise&quot;, &quot;Cruise&quot;, &quot;Cruise&quot;, &quot;Camaro&quot;, &quot;Camaro&quot;, &quot;Camaro&quot;, &quot;Cruise&quot;,
  &quot;Cruise&quot;, &quot;Cruise&quot;))
  Make &lt;- factor(c(&quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;, &quot;Ford&quot;,
  &quot;Chevy&quot;, &quot;Chevy&quot;, &quot;Chevy&quot;, &quot;Chevy&quot;, &quot;Chevy&quot;, &quot;Chevy&quot;, &quot;Toyota&quot;, &quot;Toyota&quot;, &quot;Toyota&quot;),
  levels = c(&quot;Ford&quot;, &quot;Chevy&quot;, &quot;Toyota&quot;))
  Color &lt;- factor(c(&quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;,
  &quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;),
  levels = c(&quot;Red&quot;, &quot;Blue&quot;, &quot;Green&quot;))
  Purchases &lt;- c(20, 35, 10, 32, 49, 45, 37, 53, 25, 55, 24, 13, 32, 45, 60, 12, 20, 13)
  
  Data &lt;- data.frame(Year, Model, Make, Color, Purchases)
  
  Data.order &lt;- Data %&gt;%
  group_by(Make,Model) %&gt;%
  summarize(Sum=sum(Purchases)) %&gt;%
  arrange(Make,-Sum)
  </code></pre>
  <p>The 1st level of arrangement must be the Make, and then I want to re-order the Models according to the sum of purchases of the 3 different colors. Year is not relevant for ordering. I've attached a screenshot of the end goal. I also want to add group numbers (by Make/Model).</p>
  <p><img src="https://i.stack.imgur.com/l5plw.png" alt="Intended ordering" /></p>
  <p><strong>Progress/issue:</strong> I've tried to sum and order the data into another data frame, and then reorder the levels of the primary data frame according to the new levels taken from the sum data frame, before arranging accordingly. This approach does not work because I have repeated levels in the Models from different Makes sharing the same Model name, &quot;Cruise&quot;. Perhaps I can apply a consecutive group number sequence within the new Order data frame, and then cross-match the group numbers back to primary data frame to arrange by Type, Group number? Not sure how to do this</p>
  <p>Thanks a lot everyone!!</p>