I am fairly new to R and am having some trouble with doing calculations and comparisons with dates in R. Basically, I have to data frames:
df1 <- (2921 rows)
DateTime
1 2013-06-01 00:00:00
2 2013-06-01 03:00:00
3 2013-06-01 06:00:00
4 2013-06-01 09:00:00
5 2013-06-01 12:00:00
6 2013-06-01 15:00:00
7 2013-06-01 18:00:00
8 2013-06-01 21:00:00
9 2013-06-02 00:00:00
10 2013-06-02 03:00:00
and df2 <- (70,816 rows)
Create.Date.Time Service Closing.Date.Time
1 2013-06-01 12:59:00 AV 2013-06-01 13:59:00
2 2013-06-02 07:56:00 SERVICE684793 2013-06-02 08:59:00
3 2013-06-02 09:39:00 SERVICE684793 2013-06-03 12:01:00
4 2013-06-02 14:14:00 SERVICE684796 2013-06-02 14:55:00
5 2013-06-02 17:20:00 SERVICE684797 2013-06-03 12:06:00
6 2013-06-03 07:20:00 SERVICE684793 2013-06-03 07:39:00
7 2013-06-03 08:02:00 SERVICE684839 2013-06-03 12:09:00
8 2013-06-03 08:04:00 SERVICE684841 2013-06-04 08:05:00
9 2013-06-03 08:04:00 SERVICE684841 2013-06-05 08:06:00
10 2013-06-03 08:08:00 SERVICE684841 2013-06-03 08:08:00
My task is to get the cumulative count of df2$Create.Date.time
for each i in df1$DateTime
. In other words, I am looking to count how many instances of df2$Create.Date.Time
being less than or equal to each df1$DateTime
exist.
For example, for df1$DateTime = 2013-06-02 18:00:00
, the cummulative count for df2$Create.Date.Time
would be 5 (There are 5 instances where the Create.Date.Time
is earlier than 2013-06-02 18:00:00
in df$2
).
I also need to do the same thing per service.
I have tried converting the dates (all of which are of class "POSIXct" "POSIXt"
) to seconds then doing the comparison but i keep getting weird errors. I would appreciate any help.