Skip to content
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

vec_between() #26

Closed
romainfrancois opened this issue Dec 18, 2018 · 7 comments
Closed

vec_between() #26

romainfrancois opened this issue Dec 18, 2018 · 7 comments

Comments

@romainfrancois
Copy link
Member

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)

@romainfrancois romainfrancois changed the title between vec_between() Dec 18, 2018
@DavisVaughan
Copy link
Member

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
Copy link

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
Copy link
Member

hadley commented Feb 13, 2019

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

This comment has been minimized.

@romainfrancois

This comment has been minimized.

@njtierney

This comment has been minimized.

@jayqi
Copy link

jayqi commented Apr 16, 2020

Hello,

I was told in tidyverse/dplyr#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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants