R 中的 NAND 运算符

NAND operator in R

R 中是否有 'proper' NAND 运算符,例如

nand(condition1, condition 2)

或者它只是最好的 practise/the 唯一可能做到的

!(condition1 & condition2)

还有哪些其他选择?

为了解决这个问题,R 中没有内置 nand 函数,我能想到的改进您提出的 !(x & y) 的唯一方法是将此操作移至编译语言,例如

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::LogicalVector nand(Rcpp::LogicalVector lhs, Rcpp::LogicalVector rhs) {
  R_xlen_t i = 0, n = lhs.size();
  Rcpp::LogicalVector result(n);

  for ( ; i < n; i++) {
    result[i] = !(lhs[i] && rhs[i]);
  }

  return result;
}

/*** R

Lhs <- rbinom(10e4, 1, .5)
Rhs <- rbinom(10e4, 1, .5)

r_nand <- function(x, y) !(x & y)

all.equal(nand(Lhs, Rhs), r_nand(Lhs, Rhs))
#[1] TRUE

microbenchmark::microbenchmark(
  nand(Lhs, Rhs), r_nand(Lhs, Rhs),
  times = 200L)
#Unit: microseconds
#            expr      min       lq     mean   median       uq       max neval
#  nand(Lhs, Rhs)  716.140  749.926 1215.353  771.015 1856.734  6332.284   200
#r_nand(Lhs, Rhs) 3337.494 3397.809 5106.614 3461.845 4985.807 95226.834   200

*/

这是否值得麻烦可能取决于您需要多久调用一次 nand。对于大多数用途,上述 r_nand 应该足够了。其实base::xor的实现方式类似:

base::xor
#function (x, y) 
#{
#  (x | y) & !(x & y)
#}
#<bytecode: 0x2fbbb90>
#<environment: namespace:base>