title: Find the most frequent value by row
body: <p>My problem is as follows:</p>
  <p>I have a data set containing several factor variables, which have the same categories. I need to find the category, which occurs most frequently for each row. In case of ties an arbitrary value can be chosen, although it would be great if I can have more control over it.</p>
  <p>My data set contains over a hundred factors. However, the structure is something like that:</p>
  <pre><code>df = data.frame(id = 1:3
                  var1 = c(&quot;red&quot;,&quot;yellow&quot;,&quot;green&quot;)
                  var2 = c(&quot;red&quot;,&quot;yellow&quot;,&quot;green&quot;)
                  var3 = c(&quot;yellow&quot;,&quot;orange&quot;,&quot;green&quot;)
                  var4 = c(&quot;orange&quot;,&quot;green&quot;,&quot;yellow&quot;))
  
  df
  #   id   var1   var2   var3   var4
  # 1  1    red    red yellow orange
  # 2  2 yellow yellow orange  green
  # 3  3  green  green  green yellow
  </code></pre>
  <p>The solution should be a variable within the data frame, for example var5, which contains the most frequent category for each row. It can be a factor or a numeric vector (in case the data need to be converted first to numeric vectors)</p>
  <p>In this case, I would like to have this solution:</p>
  <pre><code>df$var5
  # [1] &quot;red&quot;    &quot;yellow&quot; &quot;green&quot; 
  </code></pre>
  <p>Any advice will be much appreciated! Thanks in advance!</p>