Closed romainfrancois closed 4 years ago
Love the immediate date support
library(vctrs)
vec_between <- function(x, left, right) {
vec_compare(x, left) >= 0 & vec_compare(x, right) <= 0
}
vec_between(Sys.Date(), Sys.Date() - 1, Sys.Date() + 1)
#> [1] TRUE
# posixlt
lt <- as.POSIXlt(Sys.time())
vec_between(lt, lt - 1, lt + 1)
#> [1] TRUE
# dates and posixlt
vec_between(lt, Sys.Date() - 1, lt + 1)
#> [1] TRUE
suppressPackageStartupMessages(library(dplyr))
between(lt, lt - 1, lt + 1)
#> Error in between(lt, lt - 1, lt + 1): Not compatible with requested type: [type=list; target=double].
Created on 2018-12-18 by the reprex package (v0.2.1.9000)
Food for thought: what about considering the exlusive cases with flags?
1:5 %>% between(2, 4)
[1] FALSE TRUE TRUE TRUE FALSE
1:5 %>% between(2, 4, exclusive = TRUE)
[1] FALSE FALSE TRUE FALSE FALSE
1:5 %>% between(2, 4, exclusive_right = TRUE)
[1] FALSE TRUE TRUE FALSE FALSE
I think I'd prefer something like:
1:5 %>% between(2, 5, "[]")
1:5 %>% between(2, 5, "()")
1:5 %>% between(2, 5, "(]")
1:5 %>% between(2, 5, "[)")
It's succinct and should be recognisable to most users.
Just my two cents here - I'm not sure what the round brackets and square brackets mean? Is one of them left vs. right? Perhaps I'm missing something obvious though!
Interesting, it immediately clicked for me even though I hadn’t done maths in a long time. See e.g. the « notations for intervals » section here: https://en.m.wikipedia.org/wiki/Interval_(mathematics)
Looks like I missed something obvious - can I perhaps suggest that a summary of this is included in the documentation?
Hello,
I was told in https://github.com/tidyverse/dplyr/issues/5122 that this is the place to track when between
for character vectors will be available in dplyr. I see that this issue is closed and that an implementation is merged into the funs
package. It's not clear to me what the funs
package is, and when/if I should expect it to be available to general public users, e.g., on CRAN. Any more information would be very helpful.
Thanks!
I think vctrs has all the tools (vec_proxy_compare, ...) for a generic implementation of between, e.g.
Created on 2018-12-18 by the reprex package (v0.2.1.9000)