x | y
ベクトル化された結果を取得するために行うことができます。の最初の要素とのx || y
最初の要素のみを比較します。x
y
これを理解するには、次の点を考慮してください。
TRUE | FALSE
# [1] TRUE
TRUE || FALSE
# [1] TRUE
c(TRUE, FALSE) | c(TRUE, FALSE)
# [1] TRUE FALSE
c(TRUE, FALSE) || c(TRUE, FALSE) # only first element is compared
# [1] TRUE
c(FALSE, TRUE) | c(FALSE, TRUE)
# [1] FALSE TRUE
c(FALSE, TRUE) || c(FALSE, TRUE) # only first element is compared
# [1] FALSE
mapply
の動作を再現しているだけなので、ここでは必要ありません|
:
identical(c(FALSE, TRUE) | c(FALSE, TRUE), mapply(function(x,y) x || y, c(FALSE, TRUE),c(FALSE, TRUE)))
# [1] TRUE
identical(c(TRUE, FALSE) | c(FALSE, TRUE), mapply(function(x,y) x || y, c(TRUE, FALSE),c(FALSE, TRUE)))
# [1] TRUE
mapply
また、計算コストがはるかに高くなります。
microbenchmark::microbenchmark(mapply(function(x,y) x||y, x, y), x | y)
Unit: microseconds
expr min lq mean median uq max neval cld
mapply(function(x, y) x || y, x, y) 1495.294 1849.006 2186.77275 2012.776 2237.936 5320.702 100 b
x | y 27.713 28.868 39.97163 33.871 38.297 166.657 100 a