title: How to create a new dataframe extracting means based on subgroups given by a specific column in R
body: <p>I haven't found a good solution for this question in a while. What I want to do is to average certain rows from a dataframe based on an ID and create a differnt dataframe. Let's say I have a dataframe that looks like this:</p>
  <p><strong>Data</strong></p>
  <pre><code>structure(list(ID = c(&quot;A1&quot;, &quot;A1&quot;, &quot;A1&quot;, &quot;A1&quot;, &quot;A2&quot;, &quot;A2&quot;, &quot;A2&quot;
  ), Name = c(&quot;S.coli&quot;, &quot;S.coli&quot;, &quot;S.coli&quot;, &quot;S.coli&quot;, &quot;S.coli&quot;, 
  &quot;S.coli&quot;, &quot;S.coli&quot;), Location = c(&quot;Indv1&quot;, &quot;Indv1&quot;, &quot;Indv1&quot;, 
  &quot;Indv1&quot;, &quot;Indv2&quot;, &quot;Indv2&quot;, &quot;Indv2&quot;), x1 = c(1L, 1L, 1L, 1L, 1L, 
  1L, 1L), x2 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L), x3 = c(3L, 3L, 3L, 
  3L, 3L, 3L, 3L), x4 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L), x5 = c(5L, 
  5L, 5L, 5L, 5L, 5L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -7L))
  </code></pre>
  <pre><code>ID   Name     Location x1 x2 x3 x4 x5
  A1   S.coli   Indv1     1  2  3  4   5
  A1   S.coli   Indv1     1  2  3  4   5
  A1   S.coli   Indv1     1  2  3  4   5
  A1   S.coli   Indv1     1  2  3  4   5
  A2   S.coli   Indv2     1  2  3  4   5
  A2   S.coli   Indv2     1  2  3  4   5
  A2   S.coli   Indv2     1  2  3  4   5
  </code></pre>
  <p>Now I want a second dataframe with the average per variable x per ID code also keeping the name and location.
  Dataframe of mean values:</p>
  <pre><code>ID   Name     Location x1 x2 x3 x4 x5
  A1   S.coli   Indv1    1  2  3  4   5
  A2   S.coli   Indv2    1  2  3  4   5
  </code></pre>
  <p>I have many ID codes so subsetting and then joining the tables is almost like doing manually. I was wondering if there is a more effective way to do it.
  Thank you in advance!!</p>