0

I have a dataset that looks like this: Hospital admissions

The Id is the patient, the encounters are the individual hospital visits. The STARTENC is the start of the visit. I want to find the hospital encounters for patients that lead to a readmission within 90 days - how do I do this?

I've tried the following code:

Readmission_time <- as.POSIXlt(visit1$STARTENC)
Readmission_time <- difftime(Readmission_time[1] , Readmission_time[2:length(visit1$STARTENC)])
Readmission_time <- ifelse(Readmission_time >= 30 & Readmission_time <= 90, 1, 0)

But it returns one less observation than I have.

maydin
  • 3,715
  • 3
  • 10
  • 27
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 26 '21 at 15:09
  • Could you please include a sample of your data using `dput(your_data)`. Please don’t use images of data as they cannot be used without a lot of unnecessary effort. Questions should be reproducible [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Aug 26 '21 at 20:16

1 Answers1

0

First of all, you should make sure that your data is ordered by patient id and by STARTENC in order to be able to compare one line to the next. Secondly, you cannot just compare one line to the next as the two lines might be for different patients.

In R, one way of getting the result would be to use the data.table package.

I demonstrate the solution in the code below using a tiny dataset where you have 4 patients, and 6 encounters. Only patient A had to come back within 90 days after some encounter.

library(data.table)

# build fake data
ids <- c('D', 'C', 'A', 'A', 'A', 'B')
starts <- c('2008-01-01', '2007-04-02', '2006-02-11', '2006-03-15', '2009-02-01', '2009-03-17')
# making sure it is ordered correctly
data <- data.table(Id=ids, STARTENC=starts)[order(Id, STARTENC)]

# converting strings to Date
data[, STARTENC := as.Date(STARTENC)]

# for every patient and encounter building a column that will contain the
# date of the NEXT patient's encounter. THe by=.(Id) is important here
data[, NEXTENC := shift(STARTENC, type = 'lead'), by=.(Id)]

# computing the delta between an encounter and the next
data[, DELTA := difftime(NEXTENC, STARTENC)]

# counting how many lines have a delta below 90
data[DELTA <= 90, .N]