title: Ordering categorical variables in R
body: <p>I have a dataset with three rows: </p>
  
  <pre><code>Date         State     Count
  1994-01-05   Alabama   408
  1994-01-06   Alabama   784
  1994-02-08   Alabama   552
  1994-01-05   Alaska    1067
  1994-01-06   Alaska    36
  1994-02-08   Alaska    8571
  1994-01-05   Arizona   385
  1994-01-06   Arizona   1845
  1994-02-08   Arizona   49
  </code></pre>
  
  <p>where there are counts for the same set of dates for each of the fifty states. The dates and states are ordered as shown above.</p>
  
  <p>I want to get the date into a format with four rows*:</p>
  
  <pre><code>Date         State     Count   mean
  1994-01-05   Alabama   408     581.333
  1994-01-06   Alabama   784     581.333
  1994-02-08   Alabama   552     581.333
  1994-01-05   Arizona   385     759.666
  1994-01-06   Arizona   1845    759.666
  1994-02-08   Arizona   49      759.666
  1994-01-05   Alaska    1067    3224.666
  1994-01-06   Alaska    36      3224.666
  1994-02-08   Alaska    8571    3224.666
  </code></pre>
  
  <p>where, first, the  mean of the counts for each state is computed and inputted into the fourth column. And then, the states are reordered from smallest to largest mean.</p>
  
  <p>I was able to complete the first step of computing the mean for each state, using the command:</p>
  
  <pre><code>plyed = ddply(dataset,.(State), transform, mean= mean(Count))
  </code></pre>
  
  <p>However, this command only computed the mean for each state, but did not reorder the states by the mean value, giving the below:</p>
  
  <pre><code>Date         State     Count   mean
  1994-01-05   Alabama   408     581.333
  1994-01-06   Alabama   784     581.333
  1994-02-08   Alabama   552     581.333
  1994-01-05   Alaska    1067    3224.666
  1994-01-06   Alaska    36      3224.666
  1994-02-08   Alaska    8571    3224.666
  1994-01-05   Arizona   385     759.666
  1994-01-06   Arizona   1845    759.666
  1994-02-08   Arizona   49      759.666
  </code></pre>
  
  <p>I am unsure how to now reorder the states by their mean to get my desired output*. I tried the reorder command, but am getting all different and unwanted output formats. Here is one example of a command I tried with no success:</p>
  
  <pre><code>reorder(plyed$State, plyed$mean, order=is.ordered(plyed$State)) 
  </code></pre>