Using this code from my dataset I was able to separate out each specific ICD10Code for each PatientId:
data.code<-data.1 %>% group_by(ICD10Code,PatientId) %>%
summarise(ReferralSource=first(ReferralSource),
NextAppt=first(NextAppt), Age=max(Age),
InsuranceName=toString(unique(InsuranceName)))
ICD10Code PatientId ReferralSource NextAppt Age InsuranceName
<fct> <fct> <fct> <fct> <int> <chr>
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…
What I need to do now, and not sure how, is to loop through the ICD10Code
column and calculate the mean age from Age
column for each unique ICD10 code while keeping duplicates.
For example from the data above, ICD10Code
B351
occurs three times and the corresponding age for each B351
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?