title: How to find mode of a column values for another column in R?
body: <p>I have a sample data in R.</p>
  <pre><code>df &lt;- data.frame(year = c(&quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2020&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;, &quot;2021&quot;), type = c(&quot;circle&quot;, &quot;circle&quot;, &quot;triangle&quot;, &quot;star&quot;, &quot;circle&quot;, &quot;triangle&quot;, &quot;star&quot;))
  </code></pre>
  <p>I need to find the mode of type for each year. If type column has same number of values for a year the mode preference will be as following: star &gt; circle &gt; triangle.</p>
  <p>So my desired output will be:</p>
  <p>2020 : 'circle',</p>
  <p>2021 : 'star'</p>
  <p>I am trying something similar to this:</p>
  <pre><code>mode &lt;- function(codes){
    which.max(tabulate(codes))
  }
  
  mds &lt;- df %&gt;%
    group_by(year) %&gt;%
    summarise(mode = mode(type))
  </code></pre>
  <p>This isn't working as type column isn't numeric.</p>