title: R: How to summarize data from multiple ordered factors into one variable
body: <p>I have data representing the severity of patients' asthma symptoms under different conditions. The severity variables are ordered factors, all with the same levels (Mild &lt; Moderate &lt; Severe). Here is a simplified example:</p>
  
  <pre><code># Create example data frame
  df &lt;- data.frame(
    ID = c(1:5),
    Daytime = c("Mild", "Severe", "Mild", "Moderate", "Moderate"), # severity of daytime symptoms
    Sleep = c("Moderate", NA, "Mild", "Mild", "Moderate"), # severity of nighttime symptoms
    Activity = c("Mild", "Moderate", "Mild", "Moderate", "Severe") # severity of symptoms during activity
    )
  
  # Specify order of factor levels
  df$Daytime &lt;- ordered(
    df$Daytime,
    levels = c("Mild",
               "Moderate",
               "Severe")
    )
  df$Sleep &lt;- ordered(
    df$Sleep,
    levels = c("Mild",
               "Moderate",
               "Severe")
    )
  df$Activity &lt;- ordered(
    df$Activity,
    levels = c("Mild",
               "Moderate",
               "Severe")
  )
  
  df
  </code></pre>
  
  <p>The resulting data frame looks like this:</p>
  
  <pre><code>  ID  Daytime    Sleep Activity
  1  1     Mild Moderate     Mild
  2  2   Severe     &lt;NA&gt; Moderate
  3  3     Mild     Mild     Mild
  4  4 Moderate     Mild Moderate
  5  5 Moderate Moderate   Severe
  </code></pre>
  
  <p>I'm trying to create an "overall severity" variable where a patient's overall severity = the most severe symptoms reported in any of the three categories (Daytime, Sleep, and Activity). That is, "overall" equals the highest level from "daytime," "sleep", and "activity." The result would look like this:</p>
  
  <pre><code>  ID  Daytime    Sleep Activity  Overall
  1  1     Mild Moderate     Mild Moderate
  2  2   Severe     &lt;NA&gt; Moderate   Severe
  3  3     Mild     Mild     Mild     Mild
  4  4 Moderate     Mild Moderate Moderate
  5  5 Moderate Moderate   Severe   Severe
  </code></pre>
  
  <p>I'd like to do it without writing some big, clunky <code>for</code> loop, but I can't figure out how. I thought maybe I could do it with <code>ave()</code>, but doesn't seem to work on multiple variables at once:</p>
  
  <pre><code>&gt; df$Overall &lt;- ave(c(df$Daytime, df$Sleep, df$Activity),
  +                 df$ID,
  +                 FUN = function(i) max (i, na.rm=T)
  +                 )
  Error in `$&lt;-.data.frame`(`*tmp*`, "Worst", value = c(2L, 3L, 1L, 2L,  : 
    replacement has 15 rows, data has 5
  </code></pre>
  
  <p>Is there an apply function that can do this?</p>