R Conditional && || & |

简介:
在R中有4种逻辑符号,&&, ||, &, |
其中&, |是判断向量中单个元素的,返回的也是多个布尔元素的向量。
而&&和||是判断整个向量的,返回单个布尔逻辑值。

例子:
> x <- 1:5
> y <- c(2,2,2,2,4)
> x>1
[1] FALSE  TRUE  TRUE  TRUE  TRUE
> y>2
[1] FALSE FALSE FALSE FALSE  TRUE
> x>1 | y>2
[1] FALSE  TRUE  TRUE  TRUE  TRUE
> x>1 & y>2
[1] FALSE FALSE FALSE FALSE  TRUE


以下用法基本用不上,你甚至可能觉得返回结果有点奇怪,其实只使用第一个逻辑值的结果:
> x>1 && y>2
[1] FALSE
> x>1 || y>2
[1] FALSE

> x>1
[1] FALSE  TRUE  TRUE  TRUE  TRUE
> x>=1
[1] TRUE TRUE TRUE TRUE TRUE
> x>1 && x>=1
[1] FALSE
> x>1 || x>=1   # 这里只用了第一个逻辑值的结果作为整个表达式的结果
[1] TRUE


常用的条件判断
ifelse(condition, a, b) returns a vector of the length of its longest argument, with elements a[i] if condition[i] is true, otherwise b[i].
如果a,b的长度小于condition的长度,则循环返回。

> x>1
[1] FALSE  TRUE  TRUE  TRUE  TRUE
> ifelse(x>1,a,b)
[1] 10  2  1  2  1

这里需要注意,condition[i]和a[i],b[i]是对应的,所以有位置关系。


> ifelse(x>1 && x>1,a,b)
[1] 10

以上返回b[1]

另一种用法是在if else表达式中。
数字0表示false, 非0表示true, 这个和ifelse(condition,a,b)用法不一样,请注意,条件中只能是长度为1的布尔逻辑值,否则只使用第一个逻辑值。
> if (0) a else b
[1] 10 11 12 13 14 15
> if (1) a else b
[1] 1 2
> if (10) a else b
[1] 1 2
使用第一个逻辑值:
> if (c(10,0)) a else b
[1] 1 2
Warning message:
In if (c(10, 0)) a else b :
  the condition has length > 1 and only the first element will be used



[参考]
2. > help("&&")

Logic                   package:base                   R Documentation

Logical Operators

Description:

     These operators act on raw, logical and number-like vectors.

Usage:

     ! x
     x & y
     x && y
     x | y
     x || y
     xor(x, y)
     
     isTRUE(x)
     
Arguments:

    x, y: raw or logical or ‘number-like’ vectors (i.e., of types
          ‘double’ (class ‘numeric’), ‘integer’ and ‘complex’)), or
          objects for which methods have been written.

Details:

     ‘!’ indicates logical negation (NOT).

     ‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
     logical OR.  The shorter form performs elementwise comparisons in
     much the same way as arithmetic operators.  The longer form
     evaluates left to right examining only the first element of each
     vector.  Evaluation proceeds only until the result is determined.
     The longer form is appropriate for programming control-flow and
     typically preferred in ‘if’ clauses.

     ‘xor’ indicates elementwise exclusive OR.

     ‘isTRUE(x)’ is an abbreviation of ‘identical(TRUE, x)’, and so is
     true if and only if ‘x’ is a length-one logical vector whose only
     element is ‘TRUE’ and which has no attributes (not even names).

     Numeric and complex vectors will be coerced to logical values,
     with zero being false and all non-zero values being true.  Raw
     vectors are handled without any coercion for ‘!’, ‘&’, ‘|’ and
     ‘xor’, with these operators being applied bitwise (so ‘!’ is the
     1s-complement).

     The operators ‘!’, ‘&’ and ‘|’ are generic functions: methods can
     be written for them individually or via the ‘Ops’ (or S4 ‘Logic’,
     see below) group generic function.  (See ‘Ops’ for how dispatch is
     computed.)

     ‘NA’ is a valid logical object.  Where a component of ‘x’ or ‘y’
     is ‘NA’, the result will be ‘NA’ if the outcome is ambiguous.  In
     other words ‘NA & TRUE’ evaluates to ‘NA’, but ‘NA & FALSE’
     evaluates to ‘FALSE’.  See the examples below.

     See Syntax for the precedence of these operators: unlike many
     other languages (including S) the AND and OR operators do not have
     the same precedence (the AND operators have higher precedence than
     the OR operators).

Value:

     For ‘!’, a logical or raw vector(for raw ‘x’) of the same length
     as ‘x’: names, dims and dimnames are copied from ‘x’, and all
     other attributes (including class) if no coercion is done.

     For ‘|’, ‘&’ and ‘xor’ a logical or raw vector. The elements of
     shorter vectors are recycled as necessary (with a ‘warning’ when
     they are recycled only _fractionally_).  The rules for determining
     the attributes of the result are rather complicated.  Most
     attributes are taken from the longer argument, the first if they
     are of the same length.  Names will be copied from the first if it
     is the same length as the answer, otherwise from the second if
     that is.  For time series, these operations are allowed only if
     the series are compatible, when the class and ‘tsp’ attribute of
     whichever is a time series (the same, if both are) are used.  For
     arrays (and an array result) the dimensions and dimnames are taken
     from first argument if it is an array, otherwise the second.

     For ‘||’, ‘&&’ and ‘isTRUE’, a length-one logical vector.

S4 methods:

     ‘!’, ‘&’ and ‘|’ are S4 generics, the latter two part of the
     ‘Logic’ group generic (and hence methods need argument names ‘e1,
     e2’).

Note:

     The elementwise operators are sometimes called as functions as
     e.g.  ‘`&`(x, y)’: see the description of how argument-matching is
     done in ‘Ops’.

References:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_.  Wadsworth & Brooks/Cole.

See Also:

     ‘TRUE’ or ‘logical’.

     ‘any’ and ‘all’ for OR and AND on many scalar arguments.

     ‘Syntax’ for operator precedence.

     ‘bitwAnd’ for bitwise versions for integer vectors.

Examples:
     y <- 1 + (x <- stats::rpois(50, lambda = 1.5) / 4 - 1)
     x[(x > 0) & (x < 1)]    # all x values between 0 and 1
     if (any(x == 0) || any(y == 0)) "zero encountered"
     
     ## construct truth tables :
     
     x <- c(NA, FALSE, TRUE)
     names(x) <- as.character(x)
     outer(x, x, "&") ## AND table
     outer(x, x, "|") ## OR  table

目录
相关文章
|
网络协议 安全 网络安全
Cisco 设备上的 IPv6 特性详解
Cisco 设备上的 IPv6 特性详解
480 3
|
移动开发 缓存 前端开发
深入理解前端路由:原理、实现与应用
本书《深入理解前端路由:原理、实现与应用》全面解析了前端路由的核心概念、工作原理及其实现方法,结合实际案例探讨了其在现代Web应用中的广泛应用,适合前端开发者和相关技术人员阅读。
|
人工智能 弹性计算 Kubernetes
【云故事探索】NO.10:厦门立马耀的数字化转型之路
厦门立马耀网络科技有限公司在数字化转型中,凭借敏锐的市场洞察和技术创新,将云计算深度融合于业务。其品牌“蝉妈妈”为中小企业提供全方位数字营销解决方案,成为行业标杆。面对快速变化的市场需求,公司通过与阿里云合作,构建高可靠性计算平台,提升效率,并利用AI技术赋能客户,推动业务多元化发展,展现了云计算在企业成长中的巨大潜力。
|
关系型数据库 MySQL Java
IDEA+Mysql调试常见异常解决办法_kaic
IDEA+Mysql调试常见异常解决办法_kaic
|
机器学习/深度学习 人工智能 安全
如何建设一支高效的人工智能团队
如何建设一支高效的人工智能团队
1341 0
如何建设一支高效的人工智能团队
|
测试技术
常见测试术语
提供和维护一个主测试计划( Master Testing Plan ),包含所有预期的测试活动和测试交付物。综合的测试计划应当结合总的项目和程序开发计划,并保证资源和责任在项目中尽可能早的被了解和分配。
1924 0
|
存储 监控 Kubernetes
Docker学习路线10:容器安全
容器安全是实施和管理像Docker这样的容器技术的关键方面。它包括一组实践、工具和技术,旨在保护容器化应用程序及其运行的基础架构。在本节中,我们将讨论一些关键的容器安全考虑因素、最佳实践和建议。
970 1
|
存储 关系型数据库 Java
IP地址处理攻略:数据库中的存储与转换方法
IP地址处理攻略:数据库中的存储与转换方法
713 0
|
人工智能 Dart 算法
Flutter AI版本五子棋
在上一篇文章中,讲解了如何实现双人在本地对战的五子棋,但是只有一个人的时候就不太好玩,同时博主也没有把五子棋相关的文章写过瘾。那么这篇文章,我们来实现一个功能更加丰富的五子棋吧!
|
算法 Linux C语言
一文搞懂内核模块依赖
一文搞懂内核模块依赖