ox examples

Dawid Kałędkowski

12/9/2021

Switching single value using ox

Replace if NULL

library(ox)
x <- NULL
y <- 1
  
# if (!is.null(x)) x else y
xo(is.null, x, .else = y)
## [1] 1

Replace if infinite

library(ox)
x <- Inf
y <- 1
  
# if (is.finite(x)) x else y
xo(is.infinite, x, .else = y)
## [1] 1

Replace if character(0)

x <- character(0)
y <- 1

# if (!identical(x, character(0))) x else y
xo(identical, x, character(0), .else = y)
## [1] 1

Replace if not class

x <- structure(0, class = "test")
y <- 1

# if (inherits(x, "test")) x else y
ox(inherits, x, "test", .else = y)
## [1] 0
## attr(,"class")
## [1] "test"

Return larger value

x <- 1
y <- 2

# if (x > y) x else y
ox(`>`, x, y)
## [1] 2

Replace if doesn’t contain a text

x <- "example text"
y <- "alternative text"

# if (grepl("text", x)) x else y
ox(grepl, "text", x, .else = y)
## [1] "text"

if not a single character

x <- c("a", "b")
y <- "a"

# if (is.character(x) && length(x) == 1) x else y
ox(function(x) is.character(x) && length(x) == 1, x, .else = y)
## [1] "a"

combining with checkmate

if (require(checkmate)) {
  x <- c("a", "b")
  y <- "a"
  
  # if (test_character(x, len = 2)) x else y
  ox(test_character, x, len = 2, .else = y)  
}
## Loading required package: checkmate
## [1] "a" "b"

using magrittr pipe

library(magrittr)
x <- "a"
y <- 1

# x %<>% if (is.numeric(.)) . else y
x %<>% ox(.f = is.numeric, .else = y)

Switching vector values using OX

Replace NA with single value

x <- c(1, NA, 3)
y <- 2

# x[is.na(x)] <- y[is.na(x)]
XO(is.na, x, .else = y)
## [1] 1 2 3

Replace minimum with value

x <- c(1, 2, 3)
y <- 0

x[which.min(x)] <- y
XO(which.min, x, .else = y)
## [1] 0 2 3

Get larger value from two vectors

x <- c(1, 2, 3)
y <- c(4, 3, 2)

# x[x < y] <- y [x < y]
OX(`>`, x, y)
## [1] 4 3 3

Keep elements matching a condition

x <- c(1, 2, 3, 4)

# x[x > 2]
OX(`>`, x, 2, .else = NULL)
## [1] 3 4

Keep list elements matching a condition

x <- list(1, 2, 3, 4)

# x[vapply(x, function(xx) xx > 2, logical(1))]
OX(`>`, x, 2, .else = NULL)
## [[1]]
## [1] 3
## 
## [[2]]
## [1] 4

Replace values with dplyr::mutate

library(dplyr)
## 
## 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
iris %>%
  mutate(x = ifelse(Sepal.Length > Petal.Length, Sepal.Length, Petal.Length))
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   x
## 1            5.1         3.5          1.4         0.2     setosa 5.1
## 2            4.9         3.0          1.4         0.2     setosa 4.9
## 3            4.7         3.2          1.3         0.2     setosa 4.7
## 4            4.6         3.1          1.5         0.2     setosa 4.6
## 5            5.0         3.6          1.4         0.2     setosa 5.0
## 6            5.4         3.9          1.7         0.4     setosa 5.4
## 7            4.6         3.4          1.4         0.3     setosa 4.6
## 8            5.0         3.4          1.5         0.2     setosa 5.0
## 9            4.4         2.9          1.4         0.2     setosa 4.4
## 10           4.9         3.1          1.5         0.1     setosa 4.9
## 11           5.4         3.7          1.5         0.2     setosa 5.4
## 12           4.8         3.4          1.6         0.2     setosa 4.8
## 13           4.8         3.0          1.4         0.1     setosa 4.8
## 14           4.3         3.0          1.1         0.1     setosa 4.3
## 15           5.8         4.0          1.2         0.2     setosa 5.8
## 16           5.7         4.4          1.5         0.4     setosa 5.7
## 17           5.4         3.9          1.3         0.4     setosa 5.4
## 18           5.1         3.5          1.4         0.3     setosa 5.1
## 19           5.7         3.8          1.7         0.3     setosa 5.7
## 20           5.1         3.8          1.5         0.3     setosa 5.1
## 21           5.4         3.4          1.7         0.2     setosa 5.4
## 22           5.1         3.7          1.5         0.4     setosa 5.1
## 23           4.6         3.6          1.0         0.2     setosa 4.6
## 24           5.1         3.3          1.7         0.5     setosa 5.1
## 25           4.8         3.4          1.9         0.2     setosa 4.8
## 26           5.0         3.0          1.6         0.2     setosa 5.0
## 27           5.0         3.4          1.6         0.4     setosa 5.0
## 28           5.2         3.5          1.5         0.2     setosa 5.2
## 29           5.2         3.4          1.4         0.2     setosa 5.2
## 30           4.7         3.2          1.6         0.2     setosa 4.7
## 31           4.8         3.1          1.6         0.2     setosa 4.8
## 32           5.4         3.4          1.5         0.4     setosa 5.4
## 33           5.2         4.1          1.5         0.1     setosa 5.2
## 34           5.5         4.2          1.4         0.2     setosa 5.5
## 35           4.9         3.1          1.5         0.2     setosa 4.9
## 36           5.0         3.2          1.2         0.2     setosa 5.0
## 37           5.5         3.5          1.3         0.2     setosa 5.5
## 38           4.9         3.6          1.4         0.1     setosa 4.9
## 39           4.4         3.0          1.3         0.2     setosa 4.4
## 40           5.1         3.4          1.5         0.2     setosa 5.1
## 41           5.0         3.5          1.3         0.3     setosa 5.0
## 42           4.5         2.3          1.3         0.3     setosa 4.5
## 43           4.4         3.2          1.3         0.2     setosa 4.4
## 44           5.0         3.5          1.6         0.6     setosa 5.0
## 45           5.1         3.8          1.9         0.4     setosa 5.1
## 46           4.8         3.0          1.4         0.3     setosa 4.8
## 47           5.1         3.8          1.6         0.2     setosa 5.1
## 48           4.6         3.2          1.4         0.2     setosa 4.6
## 49           5.3         3.7          1.5         0.2     setosa 5.3
## 50           5.0         3.3          1.4         0.2     setosa 5.0
## 51           7.0         3.2          4.7         1.4 versicolor 7.0
## 52           6.4         3.2          4.5         1.5 versicolor 6.4
## 53           6.9         3.1          4.9         1.5 versicolor 6.9
## 54           5.5         2.3          4.0         1.3 versicolor 5.5
## 55           6.5         2.8          4.6         1.5 versicolor 6.5
## 56           5.7         2.8          4.5         1.3 versicolor 5.7
## 57           6.3         3.3          4.7         1.6 versicolor 6.3
## 58           4.9         2.4          3.3         1.0 versicolor 4.9
## 59           6.6         2.9          4.6         1.3 versicolor 6.6
## 60           5.2         2.7          3.9         1.4 versicolor 5.2
## 61           5.0         2.0          3.5         1.0 versicolor 5.0
## 62           5.9         3.0          4.2         1.5 versicolor 5.9
## 63           6.0         2.2          4.0         1.0 versicolor 6.0
## 64           6.1         2.9          4.7         1.4 versicolor 6.1
## 65           5.6         2.9          3.6         1.3 versicolor 5.6
## 66           6.7         3.1          4.4         1.4 versicolor 6.7
## 67           5.6         3.0          4.5         1.5 versicolor 5.6
## 68           5.8         2.7          4.1         1.0 versicolor 5.8
## 69           6.2         2.2          4.5         1.5 versicolor 6.2
## 70           5.6         2.5          3.9         1.1 versicolor 5.6
## 71           5.9         3.2          4.8         1.8 versicolor 5.9
## 72           6.1         2.8          4.0         1.3 versicolor 6.1
## 73           6.3         2.5          4.9         1.5 versicolor 6.3
## 74           6.1         2.8          4.7         1.2 versicolor 6.1
## 75           6.4         2.9          4.3         1.3 versicolor 6.4
## 76           6.6         3.0          4.4         1.4 versicolor 6.6
## 77           6.8         2.8          4.8         1.4 versicolor 6.8
## 78           6.7         3.0          5.0         1.7 versicolor 6.7
## 79           6.0         2.9          4.5         1.5 versicolor 6.0
## 80           5.7         2.6          3.5         1.0 versicolor 5.7
## 81           5.5         2.4          3.8         1.1 versicolor 5.5
## 82           5.5         2.4          3.7         1.0 versicolor 5.5
## 83           5.8         2.7          3.9         1.2 versicolor 5.8
## 84           6.0         2.7          5.1         1.6 versicolor 6.0
## 85           5.4         3.0          4.5         1.5 versicolor 5.4
## 86           6.0         3.4          4.5         1.6 versicolor 6.0
## 87           6.7         3.1          4.7         1.5 versicolor 6.7
## 88           6.3         2.3          4.4         1.3 versicolor 6.3
## 89           5.6         3.0          4.1         1.3 versicolor 5.6
## 90           5.5         2.5          4.0         1.3 versicolor 5.5
## 91           5.5         2.6          4.4         1.2 versicolor 5.5
## 92           6.1         3.0          4.6         1.4 versicolor 6.1
## 93           5.8         2.6          4.0         1.2 versicolor 5.8
## 94           5.0         2.3          3.3         1.0 versicolor 5.0
## 95           5.6         2.7          4.2         1.3 versicolor 5.6
## 96           5.7         3.0          4.2         1.2 versicolor 5.7
## 97           5.7         2.9          4.2         1.3 versicolor 5.7
## 98           6.2         2.9          4.3         1.3 versicolor 6.2
## 99           5.1         2.5          3.0         1.1 versicolor 5.1
## 100          5.7         2.8          4.1         1.3 versicolor 5.7
## 101          6.3         3.3          6.0         2.5  virginica 6.3
## 102          5.8         2.7          5.1         1.9  virginica 5.8
## 103          7.1         3.0          5.9         2.1  virginica 7.1
## 104          6.3         2.9          5.6         1.8  virginica 6.3
## 105          6.5         3.0          5.8         2.2  virginica 6.5
## 106          7.6         3.0          6.6         2.1  virginica 7.6
## 107          4.9         2.5          4.5         1.7  virginica 4.9
## 108          7.3         2.9          6.3         1.8  virginica 7.3
## 109          6.7         2.5          5.8         1.8  virginica 6.7
## 110          7.2         3.6          6.1         2.5  virginica 7.2
## 111          6.5         3.2          5.1         2.0  virginica 6.5
## 112          6.4         2.7          5.3         1.9  virginica 6.4
## 113          6.8         3.0          5.5         2.1  virginica 6.8
## 114          5.7         2.5          5.0         2.0  virginica 5.7
## 115          5.8         2.8          5.1         2.4  virginica 5.8
## 116          6.4         3.2          5.3         2.3  virginica 6.4
## 117          6.5         3.0          5.5         1.8  virginica 6.5
## 118          7.7         3.8          6.7         2.2  virginica 7.7
## 119          7.7         2.6          6.9         2.3  virginica 7.7
## 120          6.0         2.2          5.0         1.5  virginica 6.0
## 121          6.9         3.2          5.7         2.3  virginica 6.9
## 122          5.6         2.8          4.9         2.0  virginica 5.6
## 123          7.7         2.8          6.7         2.0  virginica 7.7
## 124          6.3         2.7          4.9         1.8  virginica 6.3
## 125          6.7         3.3          5.7         2.1  virginica 6.7
## 126          7.2         3.2          6.0         1.8  virginica 7.2
## 127          6.2         2.8          4.8         1.8  virginica 6.2
## 128          6.1         3.0          4.9         1.8  virginica 6.1
## 129          6.4         2.8          5.6         2.1  virginica 6.4
## 130          7.2         3.0          5.8         1.6  virginica 7.2
## 131          7.4         2.8          6.1         1.9  virginica 7.4
## 132          7.9         3.8          6.4         2.0  virginica 7.9
## 133          6.4         2.8          5.6         2.2  virginica 6.4
## 134          6.3         2.8          5.1         1.5  virginica 6.3
## 135          6.1         2.6          5.6         1.4  virginica 6.1
## 136          7.7         3.0          6.1         2.3  virginica 7.7
## 137          6.3         3.4          5.6         2.4  virginica 6.3
## 138          6.4         3.1          5.5         1.8  virginica 6.4
## 139          6.0         3.0          4.8         1.8  virginica 6.0
## 140          6.9         3.1          5.4         2.1  virginica 6.9
## 141          6.7         3.1          5.6         2.4  virginica 6.7
## 142          6.9         3.1          5.1         2.3  virginica 6.9
## 143          5.8         2.7          5.1         1.9  virginica 5.8
## 144          6.8         3.2          5.9         2.3  virginica 6.8
## 145          6.7         3.3          5.7         2.5  virginica 6.7
## 146          6.7         3.0          5.2         2.3  virginica 6.7
## 147          6.3         2.5          5.0         1.9  virginica 6.3
## 148          6.5         3.0          5.2         2.0  virginica 6.5
## 149          6.2         3.4          5.4         2.3  virginica 6.2
## 150          5.9         3.0          5.1         1.8  virginica 5.9
iris %>% mutate(x = OX(`>`, Sepal.Length, Petal.Length))
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   x
## 1            5.1         3.5          1.4         0.2     setosa 5.1
## 2            4.9         3.0          1.4         0.2     setosa 4.9
## 3            4.7         3.2          1.3         0.2     setosa 4.7
## 4            4.6         3.1          1.5         0.2     setosa 4.6
## 5            5.0         3.6          1.4         0.2     setosa 5.0
## 6            5.4         3.9          1.7         0.4     setosa 5.4
## 7            4.6         3.4          1.4         0.3     setosa 4.6
## 8            5.0         3.4          1.5         0.2     setosa 5.0
## 9            4.4         2.9          1.4         0.2     setosa 4.4
## 10           4.9         3.1          1.5         0.1     setosa 4.9
## 11           5.4         3.7          1.5         0.2     setosa 5.4
## 12           4.8         3.4          1.6         0.2     setosa 4.8
## 13           4.8         3.0          1.4         0.1     setosa 4.8
## 14           4.3         3.0          1.1         0.1     setosa 4.3
## 15           5.8         4.0          1.2         0.2     setosa 5.8
## 16           5.7         4.4          1.5         0.4     setosa 5.7
## 17           5.4         3.9          1.3         0.4     setosa 5.4
## 18           5.1         3.5          1.4         0.3     setosa 5.1
## 19           5.7         3.8          1.7         0.3     setosa 5.7
## 20           5.1         3.8          1.5         0.3     setosa 5.1
## 21           5.4         3.4          1.7         0.2     setosa 5.4
## 22           5.1         3.7          1.5         0.4     setosa 5.1
## 23           4.6         3.6          1.0         0.2     setosa 4.6
## 24           5.1         3.3          1.7         0.5     setosa 5.1
## 25           4.8         3.4          1.9         0.2     setosa 4.8
## 26           5.0         3.0          1.6         0.2     setosa 5.0
## 27           5.0         3.4          1.6         0.4     setosa 5.0
## 28           5.2         3.5          1.5         0.2     setosa 5.2
## 29           5.2         3.4          1.4         0.2     setosa 5.2
## 30           4.7         3.2          1.6         0.2     setosa 4.7
## 31           4.8         3.1          1.6         0.2     setosa 4.8
## 32           5.4         3.4          1.5         0.4     setosa 5.4
## 33           5.2         4.1          1.5         0.1     setosa 5.2
## 34           5.5         4.2          1.4         0.2     setosa 5.5
## 35           4.9         3.1          1.5         0.2     setosa 4.9
## 36           5.0         3.2          1.2         0.2     setosa 5.0
## 37           5.5         3.5          1.3         0.2     setosa 5.5
## 38           4.9         3.6          1.4         0.1     setosa 4.9
## 39           4.4         3.0          1.3         0.2     setosa 4.4
## 40           5.1         3.4          1.5         0.2     setosa 5.1
## 41           5.0         3.5          1.3         0.3     setosa 5.0
## 42           4.5         2.3          1.3         0.3     setosa 4.5
## 43           4.4         3.2          1.3         0.2     setosa 4.4
## 44           5.0         3.5          1.6         0.6     setosa 5.0
## 45           5.1         3.8          1.9         0.4     setosa 5.1
## 46           4.8         3.0          1.4         0.3     setosa 4.8
## 47           5.1         3.8          1.6         0.2     setosa 5.1
## 48           4.6         3.2          1.4         0.2     setosa 4.6
## 49           5.3         3.7          1.5         0.2     setosa 5.3
## 50           5.0         3.3          1.4         0.2     setosa 5.0
## 51           7.0         3.2          4.7         1.4 versicolor 7.0
## 52           6.4         3.2          4.5         1.5 versicolor 6.4
## 53           6.9         3.1          4.9         1.5 versicolor 6.9
## 54           5.5         2.3          4.0         1.3 versicolor 5.5
## 55           6.5         2.8          4.6         1.5 versicolor 6.5
## 56           5.7         2.8          4.5         1.3 versicolor 5.7
## 57           6.3         3.3          4.7         1.6 versicolor 6.3
## 58           4.9         2.4          3.3         1.0 versicolor 4.9
## 59           6.6         2.9          4.6         1.3 versicolor 6.6
## 60           5.2         2.7          3.9         1.4 versicolor 5.2
## 61           5.0         2.0          3.5         1.0 versicolor 5.0
## 62           5.9         3.0          4.2         1.5 versicolor 5.9
## 63           6.0         2.2          4.0         1.0 versicolor 6.0
## 64           6.1         2.9          4.7         1.4 versicolor 6.1
## 65           5.6         2.9          3.6         1.3 versicolor 5.6
## 66           6.7         3.1          4.4         1.4 versicolor 6.7
## 67           5.6         3.0          4.5         1.5 versicolor 5.6
## 68           5.8         2.7          4.1         1.0 versicolor 5.8
## 69           6.2         2.2          4.5         1.5 versicolor 6.2
## 70           5.6         2.5          3.9         1.1 versicolor 5.6
## 71           5.9         3.2          4.8         1.8 versicolor 5.9
## 72           6.1         2.8          4.0         1.3 versicolor 6.1
## 73           6.3         2.5          4.9         1.5 versicolor 6.3
## 74           6.1         2.8          4.7         1.2 versicolor 6.1
## 75           6.4         2.9          4.3         1.3 versicolor 6.4
## 76           6.6         3.0          4.4         1.4 versicolor 6.6
## 77           6.8         2.8          4.8         1.4 versicolor 6.8
## 78           6.7         3.0          5.0         1.7 versicolor 6.7
## 79           6.0         2.9          4.5         1.5 versicolor 6.0
## 80           5.7         2.6          3.5         1.0 versicolor 5.7
## 81           5.5         2.4          3.8         1.1 versicolor 5.5
## 82           5.5         2.4          3.7         1.0 versicolor 5.5
## 83           5.8         2.7          3.9         1.2 versicolor 5.8
## 84           6.0         2.7          5.1         1.6 versicolor 6.0
## 85           5.4         3.0          4.5         1.5 versicolor 5.4
## 86           6.0         3.4          4.5         1.6 versicolor 6.0
## 87           6.7         3.1          4.7         1.5 versicolor 6.7
## 88           6.3         2.3          4.4         1.3 versicolor 6.3
## 89           5.6         3.0          4.1         1.3 versicolor 5.6
## 90           5.5         2.5          4.0         1.3 versicolor 5.5
## 91           5.5         2.6          4.4         1.2 versicolor 5.5
## 92           6.1         3.0          4.6         1.4 versicolor 6.1
## 93           5.8         2.6          4.0         1.2 versicolor 5.8
## 94           5.0         2.3          3.3         1.0 versicolor 5.0
## 95           5.6         2.7          4.2         1.3 versicolor 5.6
## 96           5.7         3.0          4.2         1.2 versicolor 5.7
## 97           5.7         2.9          4.2         1.3 versicolor 5.7
## 98           6.2         2.9          4.3         1.3 versicolor 6.2
## 99           5.1         2.5          3.0         1.1 versicolor 5.1
## 100          5.7         2.8          4.1         1.3 versicolor 5.7
## 101          6.3         3.3          6.0         2.5  virginica 6.3
## 102          5.8         2.7          5.1         1.9  virginica 5.8
## 103          7.1         3.0          5.9         2.1  virginica 7.1
## 104          6.3         2.9          5.6         1.8  virginica 6.3
## 105          6.5         3.0          5.8         2.2  virginica 6.5
## 106          7.6         3.0          6.6         2.1  virginica 7.6
## 107          4.9         2.5          4.5         1.7  virginica 4.9
## 108          7.3         2.9          6.3         1.8  virginica 7.3
## 109          6.7         2.5          5.8         1.8  virginica 6.7
## 110          7.2         3.6          6.1         2.5  virginica 7.2
## 111          6.5         3.2          5.1         2.0  virginica 6.5
## 112          6.4         2.7          5.3         1.9  virginica 6.4
## 113          6.8         3.0          5.5         2.1  virginica 6.8
## 114          5.7         2.5          5.0         2.0  virginica 5.7
## 115          5.8         2.8          5.1         2.4  virginica 5.8
## 116          6.4         3.2          5.3         2.3  virginica 6.4
## 117          6.5         3.0          5.5         1.8  virginica 6.5
## 118          7.7         3.8          6.7         2.2  virginica 7.7
## 119          7.7         2.6          6.9         2.3  virginica 7.7
## 120          6.0         2.2          5.0         1.5  virginica 6.0
## 121          6.9         3.2          5.7         2.3  virginica 6.9
## 122          5.6         2.8          4.9         2.0  virginica 5.6
## 123          7.7         2.8          6.7         2.0  virginica 7.7
## 124          6.3         2.7          4.9         1.8  virginica 6.3
## 125          6.7         3.3          5.7         2.1  virginica 6.7
## 126          7.2         3.2          6.0         1.8  virginica 7.2
## 127          6.2         2.8          4.8         1.8  virginica 6.2
## 128          6.1         3.0          4.9         1.8  virginica 6.1
## 129          6.4         2.8          5.6         2.1  virginica 6.4
## 130          7.2         3.0          5.8         1.6  virginica 7.2
## 131          7.4         2.8          6.1         1.9  virginica 7.4
## 132          7.9         3.8          6.4         2.0  virginica 7.9
## 133          6.4         2.8          5.6         2.2  virginica 6.4
## 134          6.3         2.8          5.1         1.5  virginica 6.3
## 135          6.1         2.6          5.6         1.4  virginica 6.1
## 136          7.7         3.0          6.1         2.3  virginica 7.7
## 137          6.3         3.4          5.6         2.4  virginica 6.3
## 138          6.4         3.1          5.5         1.8  virginica 6.4
## 139          6.0         3.0          4.8         1.8  virginica 6.0
## 140          6.9         3.1          5.4         2.1  virginica 6.9
## 141          6.7         3.1          5.6         2.4  virginica 6.7
## 142          6.9         3.1          5.1         2.3  virginica 6.9
## 143          5.8         2.7          5.1         1.9  virginica 5.8
## 144          6.8         3.2          5.9         2.3  virginica 6.8
## 145          6.7         3.3          5.7         2.5  virginica 6.7
## 146          6.7         3.0          5.2         2.3  virginica 6.7
## 147          6.3         2.5          5.0         1.9  virginica 6.3
## 148          6.5         3.0          5.2         2.0  virginica 6.5
## 149          6.2         3.4          5.4         2.3  virginica 6.2
## 150          5.9         3.0          5.1         1.8  virginica 5.9