Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 120
4.4.3 Logical Operators
ОглавлениеLogical Operators combine vectors element by element. While logical operators can be applied directly on composite types, theymust be able to act on numeric, logical or complex types in order to produce understandable results.
operator – logical
v1 <- c(TRUE, TRUE, FALSE, FALSE) v2 <- c(TRUE, FALSE, FALSE, TRUE) v1 & v2 # and ## [1] TRUE FALSE FALSE FALSE v1 | v2 # or ## [1] TRUE TRUE FALSE TRUE !v1 # not ## [1] FALSE FALSE TRUE TRUE v1 && v2 # and applied to the first element ## [1] TRUE v1 || v2 # or applied to the first element ## [1] TRUE v1 <- c(TRUE,FALSE,TRUE,FALSE,8,6+3i,-2,, NA) class(v1) # v1 is a vector or complex numbers ## [1] “complex” v2 <- c(TRUE) as.logical(v1) # coerce to logical (only 0 is FALSE) ## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 & v2 ## [1] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE NA v1 | v2 ## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE