5  逻辑运算的操纵

5.1 逻辑/布尔运算符

Logical operator in R Description
& Elementwise logical ‘AND’
&& Vector logical ‘AND’
| Elementwise logical ‘OR’
|| Vector logical ‘OR’
! Logical negation ‘NOT’
xor() Elementwise exclusive ‘OR’ equivalent to !( x | y)

Show the code
40 & 5 > 30 # FALSE
#> [1] FALSE
40 | 5 > 30 # TRUE
#> [1] TRUE
!TRUE  # FALSE
#> [1] FALSE
!FALSE # TRUE
#> [1] TRUE

#---------
# Vectors
#---------

x <- c(3, 4, 5)
y <- c(3, 5, 1)


x & y   # TRUE TRUE TRUE
#> [1] TRUE TRUE TRUE
#x && y  # TRUE  error

x | y   # TRUE TRUE TRUE
#> [1] TRUE TRUE TRUE
#x || y  # TRUE  error

!x # FALSE FALSE FALSE
#> [1] FALSE FALSE FALSE

xor(x, y) # FALSE FALSE FALSE
#> [1] FALSE FALSE FALSE
Show the code
(x1 <- 1:10 %% 2 == 0)
#>  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
(y1 <- 1:10 %% 5 == 0)
#>  [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE

(x2 <- which(x1))
#> [1]  2  4  6  8 10
(y2 <- which(y1))
#> [1]  5 10
# 交集
x1 & y1
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
base::intersect(x2, y2)
#> [1] 10

# 并集
x1 | y1
#>  [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
base::union(x2,y2)
#> [1]  2  4  6  8 10  5

# 差集
x1 & !y1
#>  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE
base::setdiff(x2, y2)
#> [1] 2 4 6 8

# 只有一个为真
xor(x1, y1)
#>  [1] FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE
base::setdiff(base::union(x2, y2), base::intersect(x2, y2))
#> [1] 2 4 6 8 5

5.1.1 缺失值

Show the code
df <- tibble(x = c(TRUE, FALSE, NA))

df |> 
  mutate(
    and = x & NA,  #  A & B   A为TRUE,返回B ;A为FALSE,返回A
    or = x | NA    #  A | B   A为TRUE,返回A ;A为FALSE,返回B
  )
x and or
TRUE NA TRUE
FALSE FALSE NA
NA NA NA

5.2 关系运算符

Relational operator in R Description
> Greater than
< Lower than
>= Greater or equal than
<= Lower or equal than
== Equal to
!= Not equal to

5.3 逻辑比较

5.3.1 浮点比较

Show the code
x <- c(1 / 49 * 49, sqrt(2) ^ 2)
x
#> [1] 1 2
x == c(1, 2)
#> [1] FALSE FALSE
print(x, digits = 16)
#> [1] 0.9999999999999999 2.0000000000000004
near(x, c(1, 2))
#> [1] TRUE TRUE

5.3.2 缺失值

Show the code
# “contagious”
NA == NA
#> [1] NA
is.na(c(TRUE, NA, FALSE))
#> [1] FALSE  TRUE FALSE

5.4 %in%

Show the code
1:12 %in% c(1, 5, 11)
#>  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE

c(1, 2, NA) %in% NA
#> [1] FALSE FALSE  TRUE

5.5 条件转换

5.5.1 if_else()

Show the code
x <- c(-3:3, NA)

if_else(x > 0, "+ve", "-ve", missing = "???")
#> [1] "-ve" "-ve" "-ve" "-ve" "+ve" "+ve" "+ve" "???"
if_else(x == 0,
        "0", 
        if_else(x < 0, "-ve", "+ve"), "???")
#> [1] "-ve" "-ve" "-ve" "0"   "+ve" "+ve" "+ve" "???"
Show the code
x1 <- c(NA, 1, 2, NA)
y1 <- c(3, NA, 4, 6)
if_else(is.na(x1), y1, x1)
#> [1] 3 1 2 6

5.5.2 case_when()

Show the code
x <- c(-3:3, NA)
case_when(
  x == 0   ~ "0",
  x < 0    ~ "-ve", 
  x > 0    ~ "+ve",
  is.na(x) ~ "???"
)
#> [1] "-ve" "-ve" "-ve" "0"   "+ve" "+ve" "+ve" "???"

5.6 逻辑汇总

Show the code
x<-c(2,3,5,1,4)
x>2
#> [1] FALSE  TRUE  TRUE FALSE  TRUE

# 所有
all(x>2)   
#> [1] FALSE

# 存在
any(x>2)
#> [1] TRUE