Meh*_*lar 7 r logical-operators
R中的AND,OR逻辑运算符的短(&,|)和长(&&,||)形式有什么区别?
例如:
x==0 & y==1x==0 && y==1x==0 | y==1x==0 || y==1我总是在我的代码中使用简短形式.它有任何障碍吗?
&和|- 是元素方面的,可以与向量运算一起使用,||而且,并且&&始终生成单个TRUE或FALSE
差点:
> x <- 1:5
> y <- 5:1
> (x > 2) & (y < 3)
[1] FALSE FALSE FALSE TRUE TRUE
> (x > 2) && (y < 3) # here operaand && takes only 1'st elements from logical
# vectors (x>2) and (y<3)
> FALSE
Run Code Online (Sandbox Code Playgroud)
所以,&&并且||常用于if (condition) state_1 else state_2语句中,如处理长度向量1