vendredi 2 février 2018

r Conditional count based on list of repeated IDs

I cannot seem to get this to work or find the answer. I have a data frame like this:

PatientID <- c('1', "1", "1","1", "2","2","2","2","3")
hospital.time <- c(1,1,1,2,1,2,3,4,1)
fever <- c(1,1,NA,0,1,NA,1,1,NA)
ventilator<-c(1,0,1,1,0,1,0,1,NA)
df <- data.frame(PatientID, hospital.time, fever,ventilator)

Each patient have several measurements so the ID is repeated for each measurement. I would like to count how many patients in hour 1 have fever and on ventilator, how many patients have only fever, how many only on ventilator, the same for hour 2, hour 3 etc.

I have tried using boolean and dplyr based on PatientID, but no luck. Will I have to put this in a for loop to make it work?

Hope you can help.

Aucun commentaire:

Enregistrer un commentaire