Skip to content
Prev 32029 / 398506 Next

number of patients in a hospital on a given date

Heinrich
format="%d.%m.%Y")
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:
patid   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:
[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 Freq
1 2002-03-15    1
2 2002-03-16    2
3 2002-03-17    2
4 2002-03-18    1


HTH,

Marc Schwartz