Skip to content

This is a shortcut for x >= left & x <= right, implemented for local vectors and translated to the appropriate SQL for remote tables.

Usage

between(x, left, right)

Arguments

x

A vector

left, right

Boundary values. Both left and right are recycled to the size of x.

Value

A logical vector the same size as x.

Details

x, left, and right are all cast to their common type before the comparison is made.

See also

join_by() if you are looking for documentation for the between() overlap join helper.

Examples

between(1:12, 7, 9)
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
#> [12] FALSE

x <- rnorm(1e2)
x[between(x, -1, 1)]
#>  [1]  0.496786543  0.788610973  0.327939643  0.095102253  0.664095951
#>  [6] -0.864626965 -0.107118597  0.853549261 -0.812461187  0.593979747
#> [11]  0.686156832 -0.973352048  0.187170113  0.164175850  0.470210275
#> [16] -0.388739412  0.944686936 -0.314554677 -0.544363031 -0.713363693
#> [21]  0.048874092  0.592074856  0.651235615 -0.100209486 -0.694376334
#> [26]  0.772777723 -0.233525745  0.743856201 -0.004148703  0.731551786
#> [31]  0.466756495 -0.039305665 -0.859256223  0.186114388  0.080753716
#> [36] -0.641700817  0.232976691  0.878171721 -0.224428242 -0.666524840
#> [41]  0.666186061 -0.923129198  0.581075084 -0.631087798  0.400960174
#> [46] -0.225209257  0.973384949  0.589073020 -0.738191598 -0.556561229
#> [51]  0.625097480  0.305428536  0.284444921 -0.906215324  0.080477571
#> [56]  0.370830830 -0.330783698  0.414300906  0.340783564 -0.566565551
#> [61] -0.327901416 -0.956085934 -0.536002311  0.600653831  0.345734047
#> [66] -0.141477198  0.329280336 -0.925292390

# On a tibble using `filter()`
filter(starwars, between(height, 100, 150))
#> # A tibble: 5 × 14
#>   name   height  mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex   gender homew…⁵
#>   <chr>   <int> <dbl> <chr>   <chr>   <chr>     <dbl> <chr> <chr>  <chr>  
#> 1 Leia …    150    49 brown   light   brown        19 fema… femin… Aldera…
#> 2 Mon M…    150    NA auburn  fair    blue         48 fema… femin… Chandr…
#> 3 Watto     137    NA black   blue, … yellow       NA male  mascu… Toydar…
#> 4 Sebul…    112    40 none    grey, … orange       NA male  mascu… Malast…
#> 5 Gasga…    122    NA none    white,… black        NA male  mascu… Troiken
#> # … with 4 more variables: species <chr>, films <list>, vehicles <list>,
#> #   starships <list>, and abbreviated variable names ¹​hair_color,
#> #   ²​skin_color, ³​eye_color, ⁴​birth_year, ⁵​homeworld