between
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/between.html
%run nb_helpers.py
from datar.data import starwars
from datar.all import *
nb_header(between)
# https://dplyr.tidyverse.org/reference/between.html
%run nb_helpers.py
from datar.data import starwars
from datar.all import *
nb_header(between)
Try this notebook on binder.
★ between¶
Check if a value is between two other values¶
The original API:
https://dplyr.tidyverse.org/reference/between.html
Args:¶
x
: A value
left
: The left bound
right
: The right bound
inclusive
: Either both
, neither
, left
or right
.
Include boundaries. Whether to set each bound as closed or open.
Returns:¶
A bool value if x
is scalar, otherwise an array of boolean values
Note that it will be always False when NA appears in x, left or right.
In [2]:
Copied!
between(range(1, 13), 7, 9)
between(range(1, 13), 7, 9)
Out[2]:
0 False 1 False 2 False 3 False 4 False 5 False 6 True 7 True 8 True 9 False 10 False 11 False dtype: bool
In [3]:
Copied!
x = rnorm(100)
x[between(x, -1, 1)]
x = rnorm(100)
x[between(x, -1, 1)]
Out[3]:
array([ 0.83452786, 0.10189189, 0.74650094, 0.47880049, 0.55936638, -0.70022441, 0.43960076, -0.06383017, 0.28146185, -0.5502206 , 0.21524796, 0.50479523, 0.60164882, 0.51928143, -0.01063673, 0.84926787, 0.2968923 , -0.91378905, 0.75111234, -0.00124731, 0.90624557, -0.80836519, -0.64955713, 0.06907295, 0.55666354, -0.60156603, -0.18858712, -0.34637117, 0.75442128, 0.6532232 , -0.10773449, 0.39341492, -0.33727655, 0.16413264, -0.82122347, 0.85301267, 0.68354877, -0.39180811, 0.54156329, -0.87504542, -0.59525048, -0.94089336, -0.22030511, 0.13365225, -0.35710286, 0.48904832, -0.58548603, 0.11360724, -0.14562895, -0.04248941, -0.4088952 , 0.20738685, 0.16683007, -0.78370598, -0.57202633, -0.04414652, 0.10903364, -0.20484245, 0.03577897, -0.66212973, 0.15423302, -0.16953601, 0.9420302 , -0.91207817, 0.62976181])
In [4]:
Copied!
starwars >> filter(between(f.height, 100, 150))
starwars >> filter(between(f.height, 100, 150))
Out[4]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
4 | Leia Organa | 150.0 | 49.0 | brown | light | brown | 19.0 | female | feminine | Alderaan | Human |
26 | Mon Mothma | 150.0 | NaN | auburn | fair | blue | 48.0 | female | feminine | Chandrila | Human |
37 | Watto | 137.0 | NaN | black | blue, grey | yellow | NaN | male | masculine | Toydaria | Toydarian |
38 | Sebulba | 112.0 | 40.0 | none | grey, red | orange | NaN | male | masculine | Malastare | Dug |
45 | Gasgano | 122.0 | NaN | none | white, blue | black | NaN | male | masculine | Troiken | Xexto |
In [5]:
Copied!
starwars >> filter(100 <= f.height <= 150) # not as expected
starwars >> filter(100 <= f.height <= 150) # not as expected
Out[5]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
2 | R2-D2 | 96.0 | 32.0 | NaN | white, blue | red | 33.0 | none | masculine | Naboo | Droid |
4 | Leia Organa | 150.0 | 49.0 | brown | light | brown | 19.0 | female | feminine | Alderaan | Human |
7 | R5-D4 | 97.0 | 32.0 | NaN | white, red | red | NaN | none | masculine | Tatooine | Droid |
18 | Yoda | 66.0 | 17.0 | white | green | brown | 896.0 | male | masculine | NaN | Yoda's species |
26 | Mon Mothma | 150.0 | NaN | auburn | fair | blue | 48.0 | female | feminine | Chandrila | Human |
28 | Wicket Systri Warrick | 88.0 | 20.0 | brown | brown | brown | 8.0 | male | masculine | Endor | Ewok |
37 | Watto | 137.0 | NaN | black | blue, grey | yellow | NaN | male | masculine | Toydaria | Toydarian |
38 | Sebulba | 112.0 | 40.0 | none | grey, red | orange | NaN | male | masculine | Malastare | Dug |
44 | Dud Bolt | 94.0 | 45.0 | none | blue, grey | yellow | NaN | male | masculine | Vulpter | Vulptereen |
45 | Gasgano | 122.0 | NaN | none | white, blue | black | NaN | male | masculine | Troiken | Xexto |
71 | Ratts Tyerell | 79.0 | 15.0 | none | grey, blue | unknown | NaN | male | masculine | Aleen Minor | Aleena |
72 | R4-P17 | 96.0 | NaN | none | silver, red | red, blue | NaN | none | feminine | NaN | Droid |