The data will be used to check if a 1 hourly interval is within timestamp1 and timestamp2. The %within% function from library(lubridate) can do this. However, for every id and corresponding timestamps this needs to do it 24 times (0:00:000 till 23:00:000). So every id should give 24 results with TRUE or FALSE.
Here's the lubridate code that we tried:
int <- interval(timestamp1, timestamp2)
int1 <- as.POSIXct(2021-04-18 08:00:00.000),
format = "%Y-%m-%d %H:%M:%S")
Outside <- int1 %within% int
So here's the sample data:
library(lubridate)
id <- c(1, 2 ,3 ,4 ,5),
timestamp1 <- c(2021-04-18 08:03:46.000, 2021-04-19 07:06:18.000, 2021-04-21 07:07:08.000, 2021-04-22 07:51:53.000, 2021-04-22 07:43:38.000)
timestamp2 <- c(2021-04-18 13:36:40.000, 2021-04-19 10:04:40.000, 2021-04-21 11:59:05.000, 2021-04-22 11:03:22.000, 2021-04-22 10:53:11.000)
CodePudding user response:
You may do this with outer
. Here's a way with data.table::between
which should work exactly the same with lubridate::within
.
library(data.table)
f <- Vectorize(\(x, y) as.POSIXlt(x)$hour