Skip to content

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

Usage

between(x, left, right)

Arguments

x

A numeric vector of values

left, right

Boundary values (must be scalars).

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.03650705  0.30684497  0.02874623 -0.84485185 -0.60761434
#>  [6] -0.23936785 -0.56740348 -0.56640705 -0.22549041 -0.82373734
#> [11]  0.91483118 -0.12129545 -0.19458321 -0.47776550 -0.91986775
#> [16] -0.36325491 -0.59294883  0.21088142  0.84431932 -0.57917050
#> [21] -0.92145988 -0.24309986  0.38385443  0.24372838  0.77126816
#> [26]  0.49554940  0.60692501 -0.42843874 -0.14638014 -0.56439391
#> [31]  0.28345589  0.26227987 -0.88794930 -0.36486299 -0.98640649
#> [36] -0.51726894  0.03626540  0.61601710 -0.63815657  0.63194957
#> [41]  0.09478661 -0.01388449 -0.04752644 -0.25471755 -0.85468590
#> [46]  0.31105935  0.53841030  0.06174691  0.56050963  0.40836857
#> [51] -0.69764308  0.16549652  0.39627938  0.82222489  0.45623119
#> [56]  0.95984438  0.20550967  0.36959880  0.57362849 -0.52942188
#> [61]  0.33461062  0.67574602  0.87259619 -0.59750151 -0.44564571
#> [66]  0.13669216 -0.79346048 -0.06483382 -0.60200697  0.72610786
#> [71]  0.70769306  0.37536170  0.27709002 -0.62660638

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