Identifying if a datetime from one dataframe is within an hour after another dataframe

Multi tool use
Identifying if a datetime from one dataframe is within an hour after another dataframe
I have 2 dataframes, A and B:
A
abc|def|datetime |
1 |55 |2016-01-28 00:00:00 |
2 |53 |2016-01-28 00:30:00 |
3 |11 |2016-01-28 02:00:00 |
B
efg|hij|datetime |
4 |ch |2016-01-28 00:11:00 |
5 |jw |2016-01-28 11:30:00 |
6 |q1 |2016-01-28 07:00:00 |
I Want to add a true false column to the end of A if a value B is within an hour after. And also a column that shows a value B is within an hour before.
A output:
abc|def|datetime |After|Before|
1 |55 |2016-01-28 00:00:00 | 0 | 1 |
2 |53 |2016-01-28 00:30:00 | 1 | 0 |
3 |11 |2016-01-28 02:00:00 | 0 | 0 |
I have tried using `library(lubridate):
for(i in seq(nrow(B))){
for(j in seq(nrow(A))){
if(A$datetime[j] %in% seq(B$datetime[i],B$datetime[i] + hours(1))){
A$After[j] = 1
}
}
}
But i can't get it to work. Any help would be greatly appriciated.
Additional Information:
The dataframes do not line up, I am wanting to identify if there are any cases in B which are within an hour After or Before A. If this is the case then add 1 to the column. eg:
4 |ch |2016-01-28 00:11:00 |
is after 1 |55 |2016-01-28 00:00:00 |
4 |ch |2016-01-28 00:11:00 |
1 |55 |2016-01-28 00:00:00 |
4 |ch |2016-01-28 00:11:00 |
is before 2 |53 |2016-01-28 00:30:00 |
4 |ch |2016-01-28 00:11:00 |
2 |53 |2016-01-28 00:30:00 |
B is an event and I am wanting to identify that event in relation to A.
Can't get it to work how? What are you getting?
– camille
Jul 1 at 18:06
Error in seq.POSIXt(B$Datetime[i], B$Datetime[i] + : exactly two of 'to', 'by' and 'length.out' / 'along.with' must be specified
@camille– MaskedMonkey
Jul 2 at 9:24
Error in seq.POSIXt(B$Datetime[i], B$Datetime[i] + : exactly two of 'to', 'by' and 'length.out' / 'along.with' must be specified
@Onyambu as 00:30:00 is after 00:11:00
– MaskedMonkey
Jul 2 at 9:28
1 Answer
1
Edited question based on extra info. In this case it is slightly easier. You can use functions from lubridate to find our if a datetime falls between 2 dates. You need 2 functions for this. First creating an interval object and secondly using the %within%
function to find out if a date falls in an interval.
%within%
In the code below I created 2 interval objects, Before and After. To check if each value of B falls in one of the intervals we need to run a loop.
library(lubridate)
after_interval <- as.interval(3600, A$datetime)
# flip interval to get lower date first in the interval
before_interval <- int_flip(as.interval(-3600, A$datetime))
for(i in seq_along(length(B$datetime))) {
After <- B$datetime[i] %within% after_interval
After = as.integer(After)
if(i == 1){
AfterLoop = After
} else {
AfterLoop = AfterLoop + After}
Before <- B$datetime[i] %within% before_interval
Before = as.integer(Before)
if(i == 1){
BeforeLoop = Before
} else {
BeforeLoop = BeforeLoop + Before}
}
A$After <- AfterLoop
A$Before <-BeforeLoop
A
abc def datetime After Before
1 1 55 2016-01-28 00:00:00 TRUE FALSE
2 2 53 2016-01-28 00:30:00 FALSE TRUE
3 3 11 2016-01-28 02:00:00 FALSE FALSE
The two dataframes contain different information, I'll add some more to the question description.
– MaskedMonkey
Jul 2 at 9:14
@MaskedMonkey, changed the answer.
– phiver
Jul 2 at 11:06
Thank you, this appears to work well, is the 3600 seconds in an hour? does this factor in calender time, eg leap years?
– MaskedMonkey
Jul 2 at 14:51
yes 3600 seconds is an hour :-). R keeps track of leap years and by extension so do the lubridate functions. just deduct 1 hr from "2016-03-01 00:00:10" and you will get "2016-02-29 23:00:10"
– phiver
Jul 2 at 14:58
Great, as an extra question, both of my dataframes are spatial, i can create a buffer of one, how would I incorporate that the buffer of B, using package
sf
, buffer = st_buffer(B[i], 500)
and the objects A intersect as an extra restriction to making after and before true?– MaskedMonkey
Jul 2 at 15:29
sf
buffer = st_buffer(B[i], 500)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How comes for the second row it is the same day with 23hrs:30 mins after yet you have a 1?
– Onyambu
Jul 1 at 17:42