1-7 Basic Building Blocks, Workspace and Files, Sequences of Numbers, Vectors, Missing Values, Subsetting Vectors, Matrices and Data Frames
http://www.reibang.com/p/0c40795ed867?t=123
8 Logic
8.1 邏輯運(yùn)算符
- ==, <, >, <=, >=, !=, !TURE, !FALSE, &, &&(and), |, ||(or)
&/|運(yùn)算符會(huì)和vector所有元素比較歉嗓,&&/||則只和第一個(gè)元素比較
> TRUE & c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSE
> TRUE && c(TRUE, FALSE, FALSE)
[1] TRUE
邏輯運(yùn)算順序:先看AND后看OR(AND平級(jí))
5 > 8 || 6 != 8 && 4 > 3.9
## 相當(dāng)于 5 > 8 || (6 != 8 && 4 > 3.9)
8.2 邏輯判斷函數(shù)
- isTRUE()判定是否為真
- identical(x, y)判斷兩個(gè)對(duì)象是否一致
- xor(x, y)相當(dāng)于OR,判斷兩個(gè)對(duì)象中至少一個(gè)是否為真
- any(...)相當(dāng)于OR
- all(...)相當(dāng)于AND
- which()獲取“真”對(duì)象的位置/索引
> ints <- sample(10) ##sample()隨機(jī)取樣本
> 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