I am trying to calculate the average age of males in their first admission with basic R.
This is my dataset
admissions
PatientID Age Sex Admissions Year
123 25 0 1 2014
123 27 0 2 2016
456 50 1 1 2012
789 73 1 1 2007
789 81 0 4 2015
sex = 1 is Male
I'm pretty sure the average would be done this way:
mean_age <- tapply(admissions$age, admissions$sex, mean)
But I have no idea how to previously calculate the minimum. Obviously it would be their minimum age or their age on their 1st admission.
I tried to include min() in the tapply but it doesn't work, I also tried calculating it beforehand with var <- tapply
but didn't work when I tried to use the variable in the tapply for the mean.
Any help would be appreciated.
edit: The dummy data I posted is an exact replication of the dataset I have. also the original question for this exercise is: "Calculate the mean age of men at their first admission"