title: Find second maximum value in a row and paste its name in a column in R
body: <p>I have a dataset with probabilities that looks like this</p>
  
  <pre><code>       topic_1      topic_2     topic_3      topic_4      topic_5      topic_6 most_probable
  1 0.0028043479 0.0035351980 0.979083973 0.0045751502 0.0046371627 0.0053641679       topic_3
  2 0.9688616242 0.0035351980 0.013026697 0.0045751502 0.0046371627 0.0053641679       topic_1
  3 0.9928927297 0.0008069017 0.002973317 0.0010442686 0.0010584229 0.0012243603       topic_1
  4 0.9841620200 0.0017981155 0.006625797 0.0023270686 0.0023586102 0.0027283884       topic_1
  5 0.0004441958 0.0005599591 0.002063369 0.0007246827 0.9953581342 0.0008496595       topic_5
  </code></pre>
  
  <p>I find the most probable value using this function</p>
  
  <pre><code>documents.topics$most_probable &lt;- unlist(
  lapply(
    1:nrow(documents.topics),
    function(x){
    names(which.max(documents.topics[x,]))}))
  documents.topics$most_probable &lt;- as.factor(documents.topics$most_probable)
  </code></pre>
  
  <p>I wonder how I could find  second maximum value and paste its name in a new column <code>second_probable</code></p>