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.593409394 -0.377203604  0.035345008  0.463601267  0.493238059
#>  [6] -0.007099799  0.495255027  0.689946303 -0.841458911  0.974872650
#> [11]  0.558668194  0.466932369 -0.456110391 -0.921242342  0.104593642
#> [16]  0.739992568 -0.688107575 -0.188721282 -0.040002500 -0.557230173
#> [21] -0.065930568  0.247393632  0.021799681  0.401114042  0.629902609
#> [26] -0.140491030 -0.700337252 -0.723725717 -0.579167124  0.971583807
#> [31]  0.307931714 -0.957213525  0.401780375  0.878406970  0.973483399
#> [36]  0.313592138  0.768477224 -0.969379065 -0.424868076 -0.246862039
#> [41] -0.400052647 -0.029469546  0.483848081 -0.162394170 -0.814993304
#> [46]  0.810619600 -0.370033161 -0.745863175 -0.725435579  0.169996086
#> [51]  0.587254582  0.400906720 -0.154316848  0.114128808 -0.266133664
#> [56]  0.139902342  0.096621534  0.144296971 -0.341127163 -0.016655979
#> [61]  0.202838385  0.764235570 -0.793172925  0.369695455 -0.675730168
#> [66] -0.696775636 -0.898203048 -0.051854973  0.823765905 -0.169259111
#> [71]  0.972842486 -0.819999516 -0.925948176

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