RINNER Heinrich
2003-May-14 13:01 UTC
[R] number of patients in a hospital on a given date
Dear R-users! I am using R 1.7.0, under Windows XP. Having some hospital discharge data (admission date and discharge date for each patient), I want to get the number of patients in the hospital on a given date. My data look like (simple example):> x <- data.frame(patid=c("pat1", "pat2"), adm.date = c("15.03.2002","16.03.2002"), dis.date=c("18.03.2002", "17.03.2002")) I can easily do a date-time conversion from the character objects:> x[,2:3] <- apply(x[,2:3], MARGIN=2, FUN=strptime, format="%d.%m.%Y") > xpatid adm.date dis.date 1 pat1 2002-03-15 2002-03-18 2 pat2 2002-03-16 2002-03-17 What I want in the end is something like a data.frame A like this: A date no.of.patients 2002-03-14 0 2002-03-15 1 2002-03-16 2 2002-03-17 2 2002-03-18 1 2002-03-19 0 Or, alternatively, a data.frame B like this: B patid date.in.hospital pat1 2002-03-15 pat1 2002-03-16 pat1 2002-03-17 pat1 2002-03-18 pat2 2002-03-16 pat2 2002-03-17>From this I could easily get A by using "table".So the trick would be to get a data.frame with one line for each day of each patient in the hospital - but how? I'd be happy about any ideas, Heinrich Rinner.
how about this:
x <- data.frame(patid=c("pat1", "pat2"), adm.date =
c("15.03.2002","16.03.2002"),dis.date=c("18.03.2002",
"17.03.2002"))
x[,2:3] <- apply(x[,2:3], MARGIN=2, FUN=strptime,
format="%d.%m.%Y")
alldays <-
c("2002-03-14","2002-03-15","2002-03-16","2002-03-17","2002-03-18","2002-03-19")
tmp <- table(unlist(apply(x,
1,
function(y){
beg <- match(y[2],alldays)
end <- match(y[3],alldays)
alldays[beg:end]})))
nulldays <- alldays[match(alldays,names(tmp),nomatch=0)==0]
out <-
rbind(data.frame(days=c(names(tmp),nulldays),freq=c(as.numeric(tmp),rep(0,length(nulldays)))))
out[order(out$days),]
days freq
5 2002-03-14 0
1 2002-03-15 1
2 2002-03-16 2
3 2002-03-17 2
4 2002-03-18 1
6 2002-03-19 0
tomy
RINNER Heinrich <H.RINNER at tirol.gv.at> writes:
> Dear R-users!
>
> I am using R 1.7.0, under Windows XP.
>
> Having some hospital discharge data (admission date and discharge date for
> each patient), I want to get the number of patients in the hospital on a
> given date.
>
> My data look like (simple example):
>> x <- data.frame(patid=c("pat1", "pat2"),
adm.date = c("15.03.2002",
> "16.03.2002"),
> dis.date=c("18.03.2002", "17.03.2002"))
>
> I can easily do a date-time conversion from the character objects:
>> x[,2:3] <- apply(x[,2:3], MARGIN=2, FUN=strptime,
format="%d.%m.%Y")
>> x
> patid adm.date dis.date
> 1 pat1 2002-03-15 2002-03-18
> 2 pat2 2002-03-16 2002-03-17
>
> What I want in the end is something like a data.frame A like this:
> A
> date no.of.patients
> 2002-03-14 0
> 2002-03-15 1
> 2002-03-16 2
> 2002-03-17 2
> 2002-03-18 1
> 2002-03-19 0
>
> Or, alternatively, a data.frame B like this:
> B
> patid date.in.hospital
> pat1 2002-03-15
> pat1 2002-03-16
> pat1 2002-03-17
> pat1 2002-03-18
> pat2 2002-03-16
> pat2 2002-03-17
>
>>From this I could easily get A by using "table".
> So the trick would be to get a data.frame with one line for each day of
each
> patient in the hospital - but how?
>
> I'd be happy about any ideas,
> Heinrich Rinner.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
--
no signature
>-----Original Message----- >From: r-help-bounces at stat.math.ethz.ch >[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of RINNERHeinrich>Sent: Wednesday, May 14, 2003 8:02 AM >To: 'r-help at stat.math.ethz.ch' >Subject: [R] number of patients in a hospital on a given date > > >Dear R-users! > >I am using R 1.7.0, under Windows XP. > >Having some hospital discharge data (admission date and >discharge date for >each patient), I want to get the number of patients in the >hospital on a >given date. > >My data look like (simple example): >> x <- data.frame(patid=c("pat1", "pat2"), adm.date = c("15.03.2002", >"16.03.2002"), > dis.date=c("18.03.2002", "17.03.2002")) > >I can easily do a date-time conversion from the character objects: >> x[,2:3] <- apply(x[,2:3], MARGIN=2, FUN=strptime,format="%d.%m.%Y")>> x > patid adm.date dis.date >1 pat1 2002-03-15 2002-03-18 >2 pat2 2002-03-16 2002-03-17 > >What I want in the end is something like a data.frame A like this: >A >date no.of.patients >2002-03-14 0 >2002-03-15 1 >2002-03-16 2 >2002-03-17 2 >2002-03-18 1 >2002-03-19 0 > >Or, alternatively, a data.frame B like this: >B >patid date.in.hospital >pat1 2002-03-15 >pat1 2002-03-16 >pat1 2002-03-17 >pat1 2002-03-18 >pat2 2002-03-16 >pat2 2002-03-17 > >>From this I could easily get A by using "table". >So the trick would be to get a data.frame with one line for >each day of each >patient in the hospital - but how? > >I'd be happy about any ideas, >Heinrich Rinner.Heinrich, How about something like this: x <- data.frame(patid = c("pat1", "pat2"), adm.date = c("15.03.2002", "16.03.2002"), dis.date = c("18.03.2002", "17.03.2002")) x[,2:3] <- apply(x[,2:3], MARGIN = 2, FUN = strptime, format "%d.%m.%Y") x is then:> xpatid adm.date dis.date 1 pat1 2002-03-15 2002-03-18 2 pat2 2002-03-16 2002-03-17 days <- NULL # generate a combined sequence of days all patients are in the hospital # based upon the intervals from admit to discharge for (i in 1:nrow(x)) { days <- c(days, format(seq(from = x$adm.date[i], to = x$dis.date[i], by = "day"), "%Y-%m-%d")) } days is then:> days[1] "2002-03-15" "2002-03-16" "2002-03-17" "2002-03-18" "2002-03-16" "2002-03-17" # Now create a dataframe from the results of table(days) days.table <- data.frame(table(days)) days.table is then:> days.tabledays Freq 1 2002-03-15 1 2 2002-03-16 2 3 2002-03-17 2 4 2002-03-18 1 HTH, Marc Schwartz