title: Condense factor variables for duplicated ID´s in a data frame
body: <p>I have a data frame with duplicated ID´s. An ID stands for a specific entity. The ID´s are duplicated because the dataset refers to a process that every entity can go through multiple times.</p>
  
  <p>Here is a small example <code>dat</code>:</p>
  
  <pre><code>library(dplyr)
  glimpse(dat)
  Observations: 6
  Variables: 3
  $ ID      &lt;dbl&gt; 1, 1, 1, 2, 2, 2
  $ Amount  &lt;dbl&gt; 10, 70, 80, 50, 10, 10
  $ Product &lt;fct&gt; A, B, C, B, E, A
  </code></pre>
  
  <p><code>ID</code> stands for the entity, <code>Amount</code> stands for the amount of money the entity has spend and <code>Product</code> stands for the good the entity bought. </p>
  
  <p>The issue is that I have to "condense" this data. So, every ID / entity may occur only once. For the continuous variable, this is not an issue because I can simply calculate the mean per ID.</p>
  
  <pre><code>library(tidyr)
  
  dat_con_ID &lt;- dat %&gt;% 
      select(ID) %&gt;% 
      unique()
  
  dat_con_Amount &lt;- dat %&gt;% 
      group_by(ID) %&gt;% 
      summarise(Amount = mean(Amount))
  
  dat_con &lt;- inner_join(dat_con_ID, dat_con_Amount, by = "ID")
  
  glimpse(dat_con)
  Observations: 2
  Variables: 2
  $ ID     &lt;dbl&gt; 1, 2
  $ Amount &lt;dbl&gt; 53.33333, 23.33333
  </code></pre>
  
  <p>The problem is, that I can´t calculate the mean of <code>Product</code> because it´s a categorical variable. An option would be to make a dummy variable out of this factor and calculate the mean. But since the original data frame is really huge this is not a good solution. Any Idea how to handle this problem?</p>