New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request/clarification: %within% for vectors of intervals #658

Closed
aetiologicCanada opened this Issue Mar 26, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@aetiologicCanada

aetiologicCanada commented Mar 26, 2018

%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!

@vspinu

This comment has been minimized.

Member

vspinu commented Mar 26, 2018

I am labeling this as a bug. All R functions should be vectorized whenever possible. Will have a look before next release.

@vspinu vspinu added the bug label Mar 26, 2018

@aetiologicCanada

This comment has been minimized.

aetiologicCanada commented Mar 26, 2018

@aetiologicCanada

This comment has been minimized.

aetiologicCanada commented Mar 26, 2018

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]
# 
# 
@vspinu

This comment has been minimized.

Member

vspinu commented Apr 10, 2018

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 %within% to work on list of intervals.

@vspinu vspinu closed this in 87925a1 Apr 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment