title: Maximum occurrence of string from a character variable in R
body: <p>I have a data frame which has two columns(hospital name, type).Both the variables are character variables. The data is as showcased below :-</p>
  
  <pre><code>hospital_name  type
  ABC            rural
  ABC            rural
  ABC            urban
  XYZ            urban
  XYZ            urban
  EFG            rural
  </code></pre>
  
  <p>I am working on a code that will group by hospital name and take a count of each type within that group. Next, create a new column called type2 which will have the value of highest occurrence in type column. The desired out put should be :-</p>
  
  <pre><code>hospital_name  type  type2
  ABC            rural rural
  XYZ            urban urban
  EFG            rural rural        
  </code></pre>
  
  <p>I approached this problem using dplyr but I am getting error. Here is my solution :-</p>
  
  <pre><code>library("dplyr")
  df&lt;-df%&gt;%group_by(hospital_name)%&gt;%mutate(type2=names(which.max(table(type))))
  </code></pre>
  
  <p>The error is  :-</p>
  
  <pre><code>Error: incompatible types, expecting a character vector
  </code></pre>