logic_ops: Additional Logic Operators

logic_opsR Documentation

Additional Logic Operators

Description

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.

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.

Value

A logical vector.

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)

1:3 %out% 1:10
1:10 %out% 1:3


n <- c(0:5, 0:-5, 0.1, -0.1, 0, 1, Inf, -Inf, NA, NaN)
1e-20 %=numtype% "~0"
n[n %=numtype% "B"]
n[n %=numtype% "prop"]
n[n %=numtype% "I"]
n[n %=numtype% "odd"]
n[n %=numtype% "even"]
n[n %=numtype% "R"]
n[n %=numtype% "unreal"]

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




tinycodet documentation built on Sept. 12, 2024, 7:03 a.m.