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.10769471  0.87580196  0.75522441 -0.96583077  0.02155849
#>  [6] -0.05054036  0.56044248 -0.30945985 -0.05548114 -0.85242240
#> [11]  0.25381536  0.42971347 -0.50080208 -0.25382082 -0.59756293
#> [16] -0.28067940  0.21760347  0.47929487 -0.49849769 -0.38158819
#> [21]  0.10599646  0.07133988  0.09952818 -0.64165676  0.78617863
#> [26]  0.06567302  0.24314028  0.47160376 -0.91637001 -0.43950826
#> [31] -0.23747332  0.67174900  0.12393467  0.19904088  0.86802928
#> [36] -0.09909801  0.63839144  0.39417364  0.58959079  0.25863891
#> [41] -0.56755801 -0.60235590 -0.10117788 -0.88121554  0.55622372
#> [46] -0.18184584 -0.42743734  0.79919215  0.09977236 -0.82417686
#> [51] -0.56373160  0.14772181 -0.05727589 -0.17872094 -0.33802101
#> [56]  0.50788298 -0.63153401  0.15963938 -0.96049290  0.20252813
#> [61] -0.80161451 -0.26711230 -0.33216348  0.72240742  0.46963281
#> [66]  0.24724472  0.63007830 -0.81189770  0.26320166  0.41922544

# 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>