9 parity()

The function parity() determines whether a number is even or odd. This function and its related functions is.even() and is.odd() are inspired by Julia’s iseven() and isodd() functions.5 The get_even() and get_odd() functions subset a vector for its even and odd numbers respectively.

parity(mtcars$carb)
##  [1] "even" "even" "odd"  "odd"  "even" "odd"  "even" "even" "even" "even"
## [11] "even" "odd"  "odd"  "odd"  "even" "even" "even" "odd"  "even" "odd" 
## [21] "odd"  "even" "even" "even" "even" "odd"  "even" "even" "even" "even"
## [31] "even" "even"
is.even(1)
## [1] FALSE
is.even(mtcars$carb)
##  [1]  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
## [13] FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
## [25]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
is.odd(2)
## [1] FALSE
is.odd(mtcars$carb)
##  [1] FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
## [13]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
## [25] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
get_odd(mtcars$carb)
##  [1] 1 1 1 3 3 3 1 1 1 1
get_even(mtcars$carb)
##  [1] 4 4 2 4 2 2 4 4 4 4 4 2 2 2 4 2 2 2 4 6 8 2