1-7 Basic Building Blocks, Workspace and Files, Sequences of Numbers, Vectors, Missing Values, Subsetting Vectors, Matrices and Data Frames
https://www.jianshu.com/p/0c40795ed867?t=123
8 Logic
8.1 逻辑运算符
- ==, <, >, <=, >=, !=, !TURE, !FALSE, &, &&(and), |, ||(or)
&/|运算符会和vector所有元素比较,&&/||则只和第一个元素比较
> TRUE & c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSE
> TRUE && c(TRUE, FALSE, FALSE)
[1] TRUE
逻辑运算顺序:先看AND后看OR(AND平级)
5 > 8 || 6 != 8 && 4 > 3.9
## 相当于 5 > 8 || (6 != 8 && 4 > 3.9)
8.2 逻辑判断函数
- isTRUE()判定是否为真
- identical(x, y)判断两个对象是否一致
- xor(x, y)相当于OR,判断两个对象中至少一个是否为真
- any(...)相当于OR
- all(...)相当于AND
- which()获取“真”对象的位置/索引
> ints <- sample(10) ##sample()随机取样本
> ints
[1] 1 2 5 7 4 9 3 8 6 10
> ints > 5
[1] FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE
> which(ints > 7)
[1] 6 8 10
> any(ints < 0)
[1] FALSE
> all(ints > 0)
[1] TRUE