between: Do values in a numeric vector fall in specified range?

Description Usage Arguments Examples

View source: R/funs.R

Description

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

1
between(x, left, right)

Arguments

x

A numeric vector of values

left, right

Boundary values (must be scalars).

Examples

1
2
3
4
5
6
7
between(1:12, 7, 9)

x <- rnorm(1e2)
x[between(x, -1, 1)]

## Or on a tibble using filter
filter(starwars, between(height, 100, 150))

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

 [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
 [1] -0.25239677  0.24474031  0.03646412  0.19108176  0.65302203  0.73841890
 [7]  0.17456771 -0.34494095 -0.42336854 -0.07876314 -0.06363388  0.45218694
[13] -0.56582975 -0.49486470 -0.55333281 -0.39948883 -0.16306768  0.40169245
[19]  0.61785722 -0.44153070 -0.40647820 -0.43265478 -0.87159144 -0.14647821
[25]  0.71628462 -0.57132960  0.18630898 -0.39507551  0.41874033 -0.48627179
[31] -0.54921151  0.97940871  0.55188018  0.17398626  0.80770828 -0.72323668
[37] -0.11463529  0.12521575  0.29511402 -0.37259404 -0.07993349 -0.55648329
[43] -0.94351074 -0.29340151  0.11556118  0.92060878 -0.49777737  0.83677368
[49] -0.87482788  0.63003008 -0.27645568 -0.01476732 -0.37875621  0.88532615
[55] -0.07412749 -0.07797982  0.46413483  0.95777632 -0.16503904  0.10129950
[61] -0.11310266  0.37706318 -0.77641159 -0.94876681  0.04558776 -0.00649301
[67]  0.58629496  0.11830638  0.47768122
# A tibble: 5 x 13
  name  height  mass hair_color skin_color eye_color birth_year gender homeworld
  <chr>  <int> <dbl> <chr>      <chr>      <chr>          <dbl> <chr>  <chr>    
1 Leia~    150    49 brown      light      brown             19 female Alderaan 
2 Mon ~    150    NA auburn     fair       blue              48 female Chandrila
3 Watto    137    NA black      blue, grey yellow            NA male   Toydaria 
4 Sebu~    112    40 none       grey, red  orange            NA male   Malastare
5 Gasg~    122    NA none       white, bl~ black             NA male   Troiken  
# ... with 4 more variables: species <chr>, films <list>, vehicles <list>,
#   starships <list>

dplyr documentation built on June 19, 2021, 1:07 a.m.