你可以做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
Hi Thomas,tha很多 - 虽然我认为帕斯卡的解释是正确的。 – 2015-04-06 08:43:11
@RomeoKienzler不,这不是严格正确的。 – Thomas 2015-04-06 08:48:42
@Thomas是对的。 – 2015-04-06 09:09:41