Feature request/clarification: %within% for vectors of intervals #658
Labels
Comments
I am labeling this as a bug. All R functions should be vectorized whenever possible. Will have a look before next release. |
I will prepare a reproducible example.
…On Mon, 26 Mar 2018 at 11:07 Vitalie Spinu ***@***.***> wrote:
I am labeling this as a bug. All R functions should be vectorized whenever
possible. Will have a look before next release.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#658 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIPm4RUF48YQ8lFN736Onn3ZRQ7kSZPJks5tiS5LgaJpZM4S7ab4>
.
|
library(tidyverse)
library(lubridate)
testdate <- ymd("2015-01-02")
testdate
blackout<- c(interval(ymd("2014-12-30"), ymd("2014-12-31")))
blackout
testdate %within% blackout
# [1] FALSE
blackouts<- c(interval(ymd("2014-12-30"), ymd("2014-12-31")),
interval(ymd("2014-12-30"), ymd("2015-01-03")))
blackouts
testdate %within% blackouts
# [1] FALSE TRUE
#Note testdates is a multiple of blackouts
#
testdates <-c(ymd("2014-12-20", ymd("2014-12-30"), ymd("2015-01-01"), ymd("2015-01-03")))
testdates
testdates %within% blackouts
# [1] FALSE TRUE FALSE TRUE
# But 2015-01-01 is within blackout rule 2
testdates5 <-c(ymd("2014-12-20"), ymd("2014-12-30"), ymd("2015-01-01"), ymd("2015-01-03"), ymd("2015-05-05"))
testdates5
testdates5 %within% blackouts
#[1] FALSE TRUE FALSE TRUE FALSE
#Warning messages:
# 1: In as.numeric(a) - as.numeric(b@start) :
# longer object length is not a multiple of shorter object length
#2: In as.numeric(a) - as.numeric(b@start) <= b@.Data :
# longer object length is not a multiple of shorter object length
#3: In as.numeric(a) - as.numeric(b@start) :
# longer object length is not a multiple of shorter object length
# but Jan 1 2015 is within blackouts[2]
#
#
|
Ok. I have misinterpreted your question. %within% works as expected (that is, it vectorizes over both, input vector and the interval vector). Maybe we can extend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
%within% works wonderfully for the stated use case: a %within% b,
where a is a date/datetime, and b is an single interval.
But sometimes the user has a vector of intervals, even an unknown number of intervals, and the question is whether is: a %within% B, where B is a vector of intervals (b1, b2, b3...)
Example:given a set of reward blackout dates for a frequent-flyer program, can I book a flight that leaves at a particular time?
A very good solution is found here:
thanks to:https://stackoverflow.com/questions/28682816/match-dates-against-date-intervals-holiday-periods-in-multiple-years
If we assume that df$datetimes holds the flight times , and blackouts$interval holds the blackout intervals we wish to include/exclude, with one record per blackout period:
apply(sapply(df$datetime, function(x) x %within% blackouts$interval),2,any)
works just fine, but its a bit "ugly" in that way that R can be ugly.
So, two suggestions: 1) make explicit within the %within% documentation that %within%t does not work across a vector of intervals 2) perhaps offer a nice purr-ized example of the above for this specific and I suspect common situation.
As always, thanks, thanks and thanks again for the tidyverse!
The text was updated successfully, but these errors were encountered: