1
votes

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"

1
What is your expected output?Ronak Shah
If you just want the mean age for the first admission, just filter the data for only the first admission and then calculate the mean.Marinka
I'm guessing this is bogus data, but just wanted to point out the same patientID is used for two different patients (male and female). I'm guessing that's not the way it is for your real data.iod
@iod it's actually like that in the original dataset. It's confusingGabby S
If this is common in your data, ie, it re-uses the numbers of male and female patients, you might want to modify the IDs by, for example, adding .1 to male patients, just so there's a distinction.iod

1 Answers

4
votes
mean(admissions$Age[admissions$Sex==1 & admissions$Admissions==1])

This is base. There are prettier ways of doing it with dplyr or data.table. This is taking admissions's age column, filters it based on sex and Admissions both equaling 1, and calculates the mean.

A slightly tidier way still with base:

with(admissions, mean(Age[Sex==1 & Admissions==1]))

The dplyr version:

admissions %>% group_by(Sex) %>% filter(Admissions==1) %>% summarize(means=mean(Age))

This will give you a table of the mean age of each sex at first admission.