Reputation: 55
I am dealing with a time series data from a hospital using R which looks like
Time of Arrival Time of Treatment
8/1/14 12:14 AM 8/1/14 12:26 AM
8/1/14 12:22 AM 8/1/14 12:35 AM
8/1/14 12:47 AM 8/1/14 12:56 AM
8/1/14 1:07 AM 8/1/14 1:16 AM
8/1/14 1:19 AM 8/1/14 1:32 AM
8/1/14 1:53 AM 8/1/14 2:02 AM
8/1/14 1:56 AM 8/1/14 2:18 AM
8/1/14 1:58 AM 8/1/14 2:15 AM
This is essentially about the arrival of patients in ED department and when they are treated. I have data for complete one year and working on building a regression based model to predict the time taken for patients to be treated. For this, I have divided my whole dataset in a period of 20 mins (12.00 A.M to 12.20 A.M and so on) and based on this count how many patients arrived in 20 mins time frame. Currently, I am using a two step process 1. Count the number of patients between the two specific period 2. Allocating the calculated number back to the specific time period.
For performing 2nd task, I am using following code
for (i in 1:nrow(date))
{for (j in 1:nrow(period)){
if (date[i,1]>=period[j,]){
j=j+1
z[i,]=t[j,]}
}
i=i+1
}
Unfortunately, 2nd step is taking too much time to be done, I was wondering is there any efficient way by which I can perform my 2nd task ? I am new to R and using two levels of for loops is making computing time too large.
Any help in this regard will be appreciated.
Upvotes: 0
Views: 1439
Reputation: 21497
Counting how many patients are present in a given time-period i define a patient as present if:
Altering your dataset instead of 12 AM to 0 AM:
require(data.table)
text = "arr;tre
8/1/14 0:14 AM;8/1/14 0:26 AM
8/1/14 0:22 AM;8/1/14 0:35 AM
8/1/14 0:47 AM;8/1/14 0:56 AM
8/1/14 1:07 AM; 8/1/14 1:16 AM
8/1/14 1:19 AM; 8/1/14 1:32 AM
8/1/14 1:53 AM; 8/1/14 2:02 AM
8/1/14 1:56 AM;8/1/14 2:18 AM
8/1/14 1:58 AM;8/1/14 2:15 AM"
Reading and parsing data:
dat <- fread(text, sep = ";")
dat[,c("arr", "tre") := .(as.POSIXct(arr, format="%m/%d/%y %H:%M %p"), as.POSIXct(tre, format="%m/%d/%y %H:%M %p"))]
dat[,`:=`(arr_d = as.IDate(arr),
arr_t = as.ITime(arr),
tre_d = as.IDate(tre),
tre_t = as.ITime(tre))]
dat[,c("arr", "tre") := NULL]
you can use data.table
's new semi-joins. This is a new feature in data.table
1.9.7 so you need the development version. Find the installation-guidelines here.
Create a data.table
with the start and end-times of your periods:
mp <- data.table(period_start = seq(as.POSIXct("2014-08-01 0:00"), as.POSIXct("2014-08-01 03:00"), by = "20 min"))
mp <- mp[, period_end := shift(period_start, 1, type = "lead")][-.N]
mp[,`:=`(ps_d = as.IDate(period_start),
ps_t = as.ITime(period_start),
pe_d = as.IDate(period_end),
pe_t = as.ITime(period_end))]
mp[,c("period_start", "period_end") := NULL]
Run the join:
res <- dat[mp,.(ps_d, ps_t, pe_d, pe_t, x.arr_d, x.arr_t, x.tre_d, x.tre_t),
on=.(arr_d <= pe_d, arr_t <= pe_t,
tre_d >= ps_d, tre_t >= ps_t), nomatch=NA, allow.cartesian=TRUE]
Have a look at res. You can double check the categorization of the patients.
Count the number of patients by period_start
res[,sum(!is.na(x.arr_d)), by=.(ps_d, ps_t)]
This results in:
ps_d ps_t V1
1: 2014-07-31 00:00:00 1
2: 2014-07-31 00:20:00 2
3: 2014-07-31 00:40:00 1
4: 2014-07-31 01:00:00 2
5: 2014-07-31 01:20:00 1
6: 2014-07-31 01:40:00 3
7: 2014-08-01 02:00:00 3
8: 2014-08-01 02:20:00 0
9: 2014-08-01 02:40:00 0
Upvotes: 1