Roccer
Roccer

Reputation: 919

Selecting a row based on the properties of the row before

I've got the following data frame:

hospNo <- as.character(seq(1:10))
patID <- c("1","2","2","2","2","3","4","4","4","4")
admissionType <- c("chronic", "acute", "chronic", "chronic", "chronic", "acute",   "acute", "chronic", "chronic", "chronic")
dischargeDate <- c("20110101", "20100101", "20100106", "20100120", "20100314", "20120607", "20120329", "20120402", "20120408","20120421")

HospData <- cbind(hospNo,patID,admissionType,dischargeDate)
HospData <- data.frame(HospData)

I would like to select every row (hospitalization number) where a patient (patID) was going (chronic) within 14 days after an acute admission to the hospital again (discharge date).

The solution in this example would be chronic hospitalizations: 3, 8 and 9.

Is there a way to do that? I know how to change the date by as.Date ("%Y%m%d") but I post my raw data as there might be better ways to calculate.

Help would be greatly appreciated!

Upvotes: 2

Views: 143

Answers (2)

Jake Burkhead
Jake Burkhead

Reputation: 6535

> library(zoo)
> library(plyr)

> HospData$dischargeDate <- as.Date(HospData$dischargeDate, format = "%Y%m%d")
> out <- ddply(HospData, .(patID), function(x) {
    lastAcuteDischarge <- ifelse(x$admissionType == "acute", x$dischargeDate, NA)
    lastAcuteDischarge <- na.locf(lastAcuteDischarge, na.rm = FALSE)
    x[x$admissionType == "chronic" & x$dischargeDate - lastAcuteDischarge < 14,]
})
> out[complete.cases(out),]
#  hospNo patID admissionType dischargeDate
# 2      3     2       chronic    2010-01-06
# 3      8     4       chronic    2012-04-02
# 4      9     4       chronic    2012-04-08

Upvotes: 2

zx8754
zx8754

Reputation: 56004

Try this:

#data
hospNo <- as.character(seq(1:10))
patID <- c("1","2","2","2","2","3","4","4","4","4")
admissionType <- c("chronic", "acute", "chronic", "chronic", "chronic", "acute",   "acute", "chronic", "chronic", "chronic")
dischargeDate <- c("20110101", "20100101", "20100106", "20100120", "20100314", "20120607", "20120329", "20120402", "20120408","20120421")
HospData <- cbind(hospNo,patID,admissionType,dischargeDate)
HospData <- data.frame(HospData)

#merge to have acute and chronic dischargeDate on same row
HospData1 <- merge(HospData,
                   HospData[ HospData$admissionType=="acute",c("patID","dischargeDate")],
                   by="patID", all.x=T)
HospData1 <- HospData1[ HospData1$admissionType=="chronic" & 
                          !is.na(HospData1$dischargeDate.y),]
#calculate duration
HospData1$duration <- 
  as.Date(HospData1$dischargeDate.x,"%Y%m%d")-
  as.Date(HospData1$dischargeDate.y,"%Y%m%d")

#subset where within 14 days
HospData1[ HospData1$duration <15,]
#     patID hospNo admissionType dischargeDate.x dischargeDate.y duration
# 3     2      3       chronic        20100106        20100101   5 days
# 8     4      8       chronic        20120402        20120329   4 days
# 9     4      9       chronic        20120408        20120329  10 days

Upvotes: 1

Related Questions