Effective ways in Counting Number of Occurrences for a time series data using R in a specific time period
<p>I am dealing with a time series data from a hospital using R which looks like </p>

<pre><code>         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
</code></pre>

<p>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.</p>

<p>For performing 2nd task, I am using following code </p>

<pre><code>     for (i in 1:nrow(date))
      {for (j in 1:nrow(period)){
        if (date[i,1]&gt;=period[j,]){ 
        j=j+1
        z[i,]=t[j,]}
        }
          i=i+1
             }
</code></pre>

<p>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. </p>

<p>Any help in this regard will be appreciated.      </p>