tidyverse / funs

Collection of low-level functions for working with vctrs
Other
34 stars 7 forks source link

vec_between() #26

Closed romainfrancois closed 4 years ago

romainfrancois commented 5 years ago

I think vctrs has all the tools (vec_proxy_compare, ...) for a generic implementation of between, e.g.

library(vctrs)

vec_between <- function(x, left, right) {
  vec_compare(x, left) >= 0 & vec_compare(x, right) <= 0
}

vec_between(1:10, 0, 11)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
vec_between(1:10, 0, 11:20)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
vec_between(1:10, -(1:10), 11)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
vec_between(1:10, -(1:10), 11:20)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

vec_between(letters[11:20], "a", "z")
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Created on 2018-12-18 by the reprex package (v0.2.1.9000)

DavisVaughan commented 5 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)

JasonAizkalns commented 5 years ago

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
hadley commented 5 years ago

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.

njtierney commented 5 years ago

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!

romainfrancois commented 5 years ago

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)

njtierney commented 5 years ago

Looks like I missed something obvious - can I perhaps suggest that a summary of this is included in the documentation?

jayqi commented 4 years ago

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!