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.739497692 -0.799685314  0.889879869  0.909197929 -0.248567260
#>  [6] -0.831238399 -0.679457686 -0.189256849  0.110296298  0.424073520
#> [11] -0.915627086 -0.852030945  0.030228506  0.670818972 -0.007657007
#> [16] -0.268330118 -0.405502155 -0.829611184 -0.789178104  0.655318157
#> [21]  0.670252555 -0.047066336 -0.724842723 -0.066573415 -0.012196624
#> [26] -0.848794098  0.049055129 -0.161422875  0.472876871 -0.024120435
#> [31]  0.049633686 -0.484712941  0.968366303  0.050452323 -0.591036994
#> [36]  0.699726941  0.256275461  0.743493771 -0.918026451 -0.340861678
#> [41]  0.440665660 -0.484611829 -0.751240290  0.635326906 -0.302283988
#> [46]  0.642395508 -0.572406174  0.760743918 -0.855078760  0.455705468
#> [51]  0.180830796  0.891309634 -0.147884739 -0.727299390 -0.330522051
#> [56]  0.658971283  0.263277415 -0.103787163  0.767821199  0.691481434
#> [61] -0.188450691 -0.349022405  0.068052059 -0.991167956 -0.413360522
#> [66]  0.516298594  0.143169312  0.546354278 -0.074633348  0.924392129
#> [71] -0.759737256 -0.461249743  0.707310289

# On a tibble using `filter()`
filter(starwars, between(height, 100, 150))
#> # A tibble: 5 × 14
#>   name       height  mass hair_color skin_color eye_color birth_year sex  
#>   <chr>       <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>
#> 1 Leia Orga…    150    49 brown      light      brown             19 fema…
#> 2 Mon Mothma    150    NA auburn     fair       blue              48 fema…
#> 3 Watto         137    NA black      blue, grey yellow            NA male 
#> 4 Sebulba       112    40 none       grey, red  orange            NA male 
#> 5 Gasgano       122    NA none       white, bl… black             NA male 
#> # ℹ 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>