title: R: Calculating mean from a different column, keeping duplicate values
body: <p>Using this code from my dataset I was able to separate out each specific ICD10Code for each PatientId:</p>
  
  <pre><code>data.code&lt;-data.1 %&gt;% group_by(ICD10Code,PatientId) %&gt;%
    summarise(ReferralSource=first(ReferralSource),
      NextAppt=first(NextAppt), Age=max(Age),
      InsuranceName=toString(unique(InsuranceName)))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iLUZR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iLUZR.png" alt="enter image description here"></a></p>
  
  <pre><code>ICD10Code PatientId ReferralSource   NextAppt   Age InsuranceName      
   &lt;fct&gt;     &lt;fct&gt;     &lt;fct&gt;            &lt;fct&gt;    &lt;int&gt; &lt;chr&gt;              
   1 ""        397       Piedmont Hospit… N           51 SLIDING FEE SCHEDU…
   2 ""        1770      St Francis       N           42 SLIDING FEE SCHEDU…
   3 ""        9787      St Francis       Y           55 *SELF PAY*, SLIDIN…
   4 ""        18872     Piedmont Hospit… Y           50 SLIDING FEE SCHEDU…
   5 ""        20172     St Francis       Y           55 Medicaid-GA (Medic…
   6 A084      1856      Piedmont Hospit… N           35 *SELF PAY*, SLIDIN…
   7 A609      10937     Piedmont Hospit… Y           31 SLIDING FEE SCHEDU…
   8 A749      18705     St Francis       N           38 SLIDING FEE SCHEDU…
   9 B001      19100     St Francis       N           37 SLIDING FEE SCHEDU…
  10 B079      19076     St Francis       N           47 Medicaid-GA (Medic…
  11 B182      9690      St Francis       N           49 *SELF PAY*, SLIDIN…
  12 B20       18990     St Francis       N           53 Medicaid-GA (Medic…
  13 B349      20235     Piedmont Hospit… N           35 SLIDING FEE SCHEDU…
  14 B351      4781      St Francis       N           36 BCBS-GA            
  15 B351      7466      St Francis       Y           47 SLIDING FEE SCHEDU…
  16 B351      18820     Piedmont Hospit… Y           25 BCBS-GA            
  17 B353      18990     St Francis       N           53 Medicaid-GA (Medic…
  18 B370      397       Piedmont Hospit… N           51 SLIDING FEE SCHEDU…
  19 B370      19112     St Francis       Y            0 *SELF PAY*, CareSo…
  20 B370      20291     St Francis       Y            0 BCBS-GA (POS), SLI…
  </code></pre>
  
  <p>What I need to do now, and not sure how, is to loop through the <code>ICD10Code</code> column and calculate the mean age from <code>Age</code> column for each unique ICD10 code while keeping duplicates.</p>
  
  <p>For example from the data above, <code>ICD10Code</code> <code>B351</code> occurs three times and the corresponding age for each <code>B351</code> is 36,47,25. I want to calculate the mean from the age for that value. I think that I need a for-loop and will need to create a new data frame consisting of Code and the mean age. How would I go about doing this?</p>