Reputation:
I am having a little bit of trouble with the below example and any help would be greatly appreciated.
I have two vectors x and y. x is a vector of length 78,725 which contains a date range from "2017-04-23 06:55:00 UTC" to "2017-04-27 17:00:52 UTC"
but the interval of time between each element differs from 1 second to a couple of hours. y is a vector of length 8640 which contains a date range from "2017-04-23 00:00:00 UTC" to "2017-04-23 23:59:50 UTC"
in increments of 10 seconds. I would like to identify which x >= y and x < y
SAMPLE DATA:
x <- as.POSIXct(c("2017-04-24 18:32:35", "2017-04-24 14:01:03", "2017-04-24 17:51:35",
"2017-04-24 15:42:22", "2017-04-24 13:00:51", "2017-04-24 16:56:28",
"2017-04-24 17:17:32", "2017-04-24 15:03:34", "2017-04-24 22:40:47",
"2017-04-23 17:37:15"), tz = "UTC")
base.date <- as.POSIXct("2017-04-23 0:00:00", tz = "UTC")
every = 10
seconds.in.day = 60*60*24
y <- seq(base.date, length = seconds.in.day / every, by = every)
RESULTS I WANT:
The 10th position of x i.e. "2017-04-23 17:37:15 UTC"
is between y element 6344 and 6345.
TRIED THE FOLLOWING:
mapply(function(x, y) x >= y & x < y, as.data.frame(x), as.data.frame(y))
and
Position(function(x) x >= y & x < y, x)
and
vapply(x, function(x) x >= y & x < y, logical(NROW(x)))
None of these have returned what I am wanting
Upvotes: 0
Views: 100
Reputation: 92
You sample data is difficult because only one of the observations in x
falls within the range of y
. So I've written the following to account for that. Give this a try...
# Filter down only to observations within the date range of y
x_range <- x[max(y) >= x & min(y) <= x]
for (i in length(x_range)) {
upper.y.index <- vector('numeric')
upper.y.index[i] <- which.max(y < x_range[i])
lower.y.index <- vector('numeric')
lower.y.index[i] <- which.max(y >= x_range[i])
}
upper.y.index
[1] 1
lower.y.index
[1] 6345
Upvotes: 0