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.77400496 -0.10965133 -0.85848691  0.61164677 -0.18216364
#>  [6]  0.64925063 -0.07592644  0.03169761  0.22534482  0.28635825
#> [11]  0.37432239  0.23346115 -0.05965820 -0.83003363 -0.70220646
#> [16]  0.03985685 -0.14198361  0.23506612  0.43643226 -0.13233908
#> [21]  0.03427724 -0.11291667 -0.35210837  0.73924160 -0.49587807
#> [26]  0.56849329 -0.19439253 -0.80253730  0.28096853 -0.53319096
#> [31]  0.20258305  0.53570527 -0.20703546 -0.83605361 -0.22611924
#> [36]  0.43536160 -0.10772829  0.43651010 -0.25694467  0.91833333
#> [41]  0.76501197 -0.57912048 -0.49780907 -0.33351255 -0.13500929
#> [46] -0.16153984  0.90094504  0.31086628 -0.11583735 -0.09376958
#> [51] -0.51018054  0.09989909 -0.43223328  0.68796175 -0.77059322
#> [56] -0.52908972  0.55291152 -0.37270921 -0.04903991 -0.54436286
#> [61] -0.99366008 -0.83638639 -0.69043297 -0.35100734  0.02117036
#> [66]  0.61721231  0.44918021 -0.16424495  0.07098008  0.72141049
#> [71] -0.02437883  0.75140592

## Or 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 
#> # … with 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>