Skip to contents

Additional logic operators:

The x %xor% y operator is the "exclusive-or" operator, the same as xor(x, y).

The x %n&% operator is the "not-and" operator, the same as (!x) & (!y).

The x %out% y operator is the same as !x %in% y.

The x %?=% y operator checks if x and y are both unreal or unknown (i.e. NA, NaN, Inf, -Inf).

The n %=numtype% numtype operator checks for every value of numeric vector n if it can be considered a number belonging to type numtype.

The s %=strtype% strtype operator checks for every value of character vector s if it can seen as a certain strtype.

Usage

x %xor% y

x %n&% y

x %out% y

x %?=% y

n %=numtype% numtype

s %=strtype% strtype

Arguments

x, y

see Logic.

n

a numeric vector.

numtype

a single string giving the numeric type to be checked.
See Details section for supported types.

s

a character vector.

strtype

a single string giving the string type to be checked.
See Details section for supported types.

Value

A logical vector.

Details

For argument numtype, the following options are supported:

  • "~0": zero, or else a number whose absolute value is smaller than the Machine tolerance (sqrt(.Machine$double.eps)).

  • "B": binary numbers (exactly 0 or exactly 1);

  • "prop": proportions - numbers between 0 and 1 (exactly 0 or 1 is also allowed);

  • "I": Integers;

  • "odd": odd integers;

  • "even": even integers;

  • "R": Real numbers;

  • "unreal": infinity, NA, or NaN;

For argument strtype, the following options are supported:

  • "empty": checks if the string only consists of empty spaces.

  • "unreal": checks if the string is NA, or if it has literal string "NA", "NaN" or "Inf", regardless if it has leading or trailing spaces.

  • "numeric": checks if the string can be converted to a number, disregarding leading and trailing spaces. I.e. the string "5.0" can be converted to the the actual number 5.0.

  • "special": checks if the string consists of only special characters.

Examples

x <- c(TRUE, FALSE, TRUE, FALSE, NA, NaN, Inf, -Inf, TRUE, FALSE)
y <- c(FALSE, TRUE, TRUE, FALSE, rep(NA, 6))
outcome <- data.frame(
  x = x, y = y,
  "x %xor% y" = x %xor% y, "x %n&% y" = x %n&% y, "x %?=% y" = x %?=% y,
  check.names = FALSE
)
print(outcome)
#>       x     y x %xor% y x %n&% y x %?=% y
#> 1     1 FALSE      TRUE    FALSE    FALSE
#> 2     0  TRUE      TRUE    FALSE    FALSE
#> 3     1  TRUE     FALSE    FALSE    FALSE
#> 4     0 FALSE     FALSE     TRUE    FALSE
#> 5    NA    NA        NA       NA     TRUE
#> 6   NaN    NA        NA       NA     TRUE
#> 7   Inf    NA        NA       NA     TRUE
#> 8  -Inf    NA        NA       NA     TRUE
#> 9     1    NA        NA       NA    FALSE
#> 10    0    NA        NA       NA    FALSE

1:3 %out% 1:10
#> [1] FALSE FALSE FALSE
1:10 %out% 1:3
#>  [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE


n <- c(0:5, 0:-5, 0.1, -0.1, 0, 1, Inf, -Inf, NA, NaN)
1e-20 %=numtype% "~0"
#> [1] TRUE
n[n %=numtype% "B"]
#> [1] 0 1 0 0 1
n[n %=numtype% "prop"]
#> [1] 0.0 1.0 0.0 0.1 0.0 1.0
n[n %=numtype% "I"]
#>  [1]  0  1  2  3  4  5  0 -1 -2 -3 -4 -5  0  1
n[n %=numtype% "odd"]
#> [1]  1  3  5 -1 -3 -5  1
n[n %=numtype% "even"]
#> [1]  0  2  4  0 -2 -4  0
n[n %=numtype% "R"]
#>  [1]  0.0  1.0  2.0  3.0  4.0  5.0  0.0 -1.0 -2.0 -3.0 -4.0 -5.0  0.1 -0.1  0.0
#> [16]  1.0
n[n %=numtype% "unreal"]
#> [1]  Inf -Inf   NA  NaN

s <- c(" AbcZ123 ", " abc ", " 1.3 ", " !#$%^&*() ", "  ", "  NA  ", "  NaN  ", " Inf ")
s[s %=strtype% "empty"]
#> [1] "  "
s[s %=strtype% "unreal"]
#> [1] "  NA  "  "  NaN  " " Inf "  
s[s %=strtype% "numeric"]
#> [1] " 1.3 " " Inf "
s[s %=strtype% "special"]
#> [1] " !#$%^&*() "