R语言比较运算符 #

一、比较运算符概述 #

比较运算符用于比较两个值,返回逻辑值TRUE或FALSE。

二、基本比较运算符 #

2.1 等于 (==) #

r
5 == 5
5 == 3
"a" == "a"
"a" == "b"

x <- c(1, 2, 3, 4, 5)
x == 3

x <- c(1, 2, 3)
y <- c(1, 2, 4)
x == y

2.2 不等于 (!=) #

r
5 != 3
5 != 5
"a" != "b"

x <- c(1, 2, 3, 4, 5)
x != 3

2.3 大于 (>) #

r
5 > 3
3 > 5
5 > 5

x <- c(1, 2, 3, 4, 5)
x > 3

2.4 小于 (<) #

r
3 < 5
5 < 3
3 < 3

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

2.5 大于等于 (>=) #

r
5 >= 5
5 >= 3
3 >= 5

x <- c(1, 2, 3, 4, 5)
x >= 3

2.6 小于等于 (<=) #

r
3 <= 3
3 <= 5
5 <= 3

x <- c(1, 2, 3, 4, 5)
x <= 3

三、向量比较 #

3.1 向量与标量 #

r
x <- c(10, 20, 30, 40, 50)

x > 25
x == 30
x != 20
x >= 30
x <= 30

3.2 向量与向量 #

r
x <- c(10, 20, 30)
y <- c(15, 20, 25)

x > y
x < y
x == y
x != y

3.3 循环补齐 #

r
x <- c(10, 20, 30)
y <- c(15, 25)

x > y

x <- c(10, 20, 30, 40)
y <- c(15, 25)

x > y

四、字符串比较 #

4.1 基本比较 #

r
"a" == "a"
"a" == "b"
"a" < "b"
"apple" < "banana"

4.2 字符串向量 #

r
fruits <- c("apple", "banana", "cherry")

fruits == "banana"
fruits != "apple"
fruits < "cherry"

4.3 大小写敏感 #

r
"a" == "A"
"A" < "a"
tolower("A") == tolower("a")

五、特殊值比较 #

5.1 NA比较 #

r
NA == NA
NA != NA
NA > 5
NA < 5

is.na(NA)
x <- c(1, NA, 3)
is.na(x)

5.2 NULL比较 #

r
NULL == NULL
NULL != NULL
is.null(NULL)

5.3 Inf比较 #

r
Inf == Inf
-Inf == -Inf
Inf > 1000000
-Inf < -1000000
is.infinite(Inf)
is.finite(100)

5.4 NaN比较 #

r
NaN == NaN
NaN != NaN
is.nan(NaN)

六、组合比较 #

6.1 逻辑与 (&) #

r
x <- c(10, 20, 30, 40, 50)

x > 20 & x < 40
x >= 20 & x <= 40

6.2 逻辑或 (|) #

r
x <- c(10, 20, 30, 40, 50)

x < 20 | x > 40
x == 20 | x == 40

6.3 逻辑非 (!) #

r
x <- c(10, 20, 30, 40, 50)

!(x > 30)
!(x == 20)

七、比较函数 #

7.1 identical函数 #

r
x <- c(1, 2, 3)
y <- c(1, 2, 3)
z <- c(1, 2, 4)

identical(x, y)
identical(x, z)

identical(1, 1L)
identical(1, as.numeric(1))

7.2 all.equal函数 #

r
x <- 0.1 + 0.2
y <- 0.3

x == y
all.equal(x, y)

all.equal(1, 1.0000001)
all.equal(1, 1.0000001, tolerance = 1e-5)

7.3 compare函数 #

r
x <- c(1, 2, 3)
y <- c(1, 2, 3)

all(x == y)
any(x != y)

八、排序比较 #

8.1 sort函数 #

r
x <- c(30, 10, 50, 20, 40)

sort(x)
sort(x, decreasing = TRUE)

8.2 order函数 #

r
x <- c(30, 10, 50, 20, 40)

order(x)
x[order(x)]

8.3 rank函数 #

r
x <- c(30, 10, 50, 20, 40)

rank(x)
rank(x, ties.method = "first")

九、极值比较 #

9.1 min和max #

r
x <- c(10, 20, 30, 40, 50)

min(x)
max(x)
range(x)

9.2 pmin和pmax #

r
x <- c(10, 20, 30)
y <- c(15, 10, 35)

pmin(x, y)
pmax(x, y)

9.3 which.min和which.max #

r
x <- c(10, 20, 30, 40, 50)

which.min(x)
which.max(x)

十、实践示例 #

10.1 数据筛选 #

r
data <- data.frame(
  name = c("张三", "李四", "王五"),
  age = c(25, 30, 22),
  score = c(85, 90, 78)
)

data[data$age > 24, ]
data[data$score >= 85, ]
data[data$age > 24 & data$score >= 85, ]

10.2 条件计数 #

r
x <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

sum(x > 50)
sum(x >= 30 & x <= 70)
mean(x > 50)

10.3 成绩等级 #

r
scores <- c(85, 60, 45, 90, 55)

grade <- ifelse(scores >= 90, "优秀",
                ifelse(scores >= 60, "及格", "不及格"))
print(grade)

10.4 数据验证 #

r
age <- c(25, -5, 150, 30, 0)

valid <- age > 0 & age < 120
print(valid)

age[!valid] <- NA
print(age)

十一、总结 #

本章学习了:

  • 基本比较运算符:==、!=、>、<、>=、<=
  • 向量比较和循环补齐
  • 字符串比较
  • 特殊值比较:NA、NULL、Inf、NaN
  • 组合比较:&、|、!
  • 比较函数:identical、all.equal
  • 排序和极值函数

比较运算符是数据筛选和条件判断的基础,在数据分析中广泛应用!

最后更新:2026-03-27