mercredi 8 février 2017

Subtracting Timestamps from different data frames

Vote count: 0

I have two datasets, one for hospital encounters, with multiple records for the same encounter for different medication order ID's:

      VISIT_KEY PAT_KEY DICT_ENC_TYPE_KEY     HOSP_DISCHRG_DT MED_ORD_ID
1      82919395 8979499                83 2014-09-07 10:47:00   58826846
2      82919395 8979499                83 2014-09-07 10:47:00   58826847
3      82919395 8979499                83 2014-09-07 10:47:00   58826848
4      82919395 8979499                83 2014-09-07 10:47:00   58826845

and one for outpatient encounters, with multiple records for the same patient if they had multiple outpatient encounters:

      VISIT_KEY PAT_KEY DICT_ENC_TYPE_KEY     APPT_CHECKIN_DT
1      84273751 8979499               108 2015-02-07 11:57:46
2      83999897 8979499               108 2014-09-13 16:51:22
3      83881023 8979499               108 2014-11-12 10:37:51
4      83999896 8979499               108 2014-11-20 09:23:25
5      95164335 8979499               108 2016-07-27 15:30:25
6      83922326 8979499               108 2014-11-16 09:08:47

I am trying to flag an entire hospital encounter if a patient had a follow-up outpatient encounter within 7 days of the hospital discharge date, including any time on the 7th day (i.e. difference between the Timestamps could be greater than 168 hours, and a patient discharged at 07/07/2012 12:00:00 and checked-in at an outpatient facility at 7/14/2012 04:00:00 would be flagged), such as below, where I would flag each record in the hospital encounter table since record [2] in the outpatient encounter meets the above criteria:

      VISIT_KEY PAT_KEY DICT_ENC_TYPE_KEY     HOSP_DISCHRG_DT MED_ORD_ID HAD_FOLLOWUP
1      82919395 8979499                83 2014-09-07 10:47:00   58826846            1
2      82919395 8979499                83 2014-09-07 10:47:00   58826847            1
3      82919395 8979499                83 2014-09-07 10:47:00   58826848            1
4      82919395 8979499                83 2014-09-07 10:47:00   58826845            1

I then would like to create a field to display the numbers of days from the discharge date to the follow-up date, such as below:

      VISIT_KEY PAT_KEY DICT_ENC_TYPE_KEY     HOSP_DISCHRG_DT MED_ORD_ID HAD_FOLLOWUP DAYS_TO_FOLLOWUP
1      82919395 8979499                83 2014-09-07 10:47:00   58826846            1               6
2      82919395 8979499                83 2014-09-07 10:47:00   58826847            1               6
3      82919395 8979499                83 2014-09-07 10:47:00   58826848            1               6
4      82919395 8979499                83 2014-09-07 10:47:00   58826845            1               6

I converted the factor Date variables to POSIXt with

test_pt_outpatient$APPT_CHECKIN_DT <- strptime(x = as.character(test_pt_outpatient$APPT_CHECKIN_DT), format = "%Y-%m-%d %H:%M:%S")
test_pt_hospital_visits$HOSP_DISCHRG_DT <- strptime(x = as.character(test_pt_hospital_visits$HOSP_DISCHRG_DT), format = "%Y-%m-%d H:%M:%S")

have been trying to work on the flag with the following:

for (i in 1:length(test_pt_outpatient$OP_ENC_DATE)) {
  ifelse(test_pt_hospital_visits$PAT_KEY[i] %in% test_pt_outpatient_visits$PAT_KEY,
     ifelse(as.numeric(test_pt_outpatient$OP_ENC_DATE[i] - test_pt_hospital_visits$HOSP_ENC_DATE[i])<7,
            print(1),print(0)))

}

but this gives me an error after I reach the end of the hospital records:

[1] 0
[1] 1
[1] 0
[1] 0
Error in ifelse(test_pt_hospital_visits$PAT_KEY[i] %in%     test_pt_outpatient_visits$PAT_KEY,  : 
  argument "no" is missing, with no default

asked 20 secs ago

Let's block ads! (Why?)



Subtracting Timestamps from different data frames

Aucun commentaire:

Enregistrer un commentaire