R语言逻辑类型 #

一、逻辑类型概述 #

R语言中的逻辑类型(Logical)只有两个值:TRUE和FALSE,用于表示真和假。

二、创建逻辑值 #

2.1 直接创建 #

r
x <- TRUE
y <- FALSE

class(x)
typeof(x)

2.2 缩写形式 #

r
x <- T
y <- F

print(x)
print(y)

2.3 比较运算产生 #

r
5 > 3
10 == 10
"a" == "b"

2.4 逻辑向量 #

r
x <- c(TRUE, FALSE, TRUE, FALSE)
y <- c(T, F, T, F)

sum(x)
mean(x)

三、逻辑运算 #

3.1 逻辑与(AND) #

r
TRUE & TRUE
TRUE & FALSE
FALSE & FALSE

c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, FALSE)

3.2 逻辑或(OR) #

r
TRUE | FALSE
FALSE | FALSE
TRUE | TRUE

c(TRUE, FALSE, FALSE) | c(FALSE, TRUE, FALSE)

3.3 逻辑非(NOT) #

r
!TRUE
!FALSE

!c(TRUE, FALSE, TRUE)

3.4 短路运算 #

r
TRUE && FALSE
FALSE && TRUE
TRUE || FALSE
FALSE || TRUE

3.5 运算符区别 #

运算符 说明
& 向量化AND,对每个元素运算
| 向量化OR,对每个元素运算
&& 短路AND,只检查第一个元素
|| 短路OR,只检查第一个元素
r
x <- c(TRUE, FALSE, TRUE)
y <- c(FALSE, TRUE, FALSE)

x & y
x && y

x | y
x || y

四、比较运算 #

4.1 基本比较 #

r
5 > 3
5 < 3
5 >= 5
5 <= 3
5 == 5
5 != 3

4.2 向量比较 #

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

4.3 字符串比较 #

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

4.4 特殊比较 #

r
NA == NA
is.na(NA)

NULL == NULL
is.null(NULL)

NaN == NaN
is.nan(NaN)

五、逻辑函数 #

5.1 all函数 #

r
x <- c(TRUE, TRUE, TRUE)
all(x)

y <- c(TRUE, FALSE, TRUE)
all(y)

all(c(1, 2, 3) > 0)

5.2 any函数 #

r
x <- c(FALSE, FALSE, TRUE)
any(x)

y <- c(FALSE, FALSE, FALSE)
any(y)

any(c(1, 2, 3) > 2)

5.3 which函数 #

r
x <- c(10, 20, 30, 40, 50)
which(x > 25)
which(x == 30)
which.max(x)
which.min(x)

5.4 ifelse函数 #

r
x <- c(1, 2, 3, 4, 5)
ifelse(x > 3, "大", "小")

scores <- c(85, 60, 45, 90, 55)
ifelse(scores >= 60, "及格", "不及格")

六、类型转换 #

6.1 转换为逻辑值 #

r
as.logical(1)
as.logical(0)
as.logical(0.5)
as.logical(-1)

as.logical("TRUE")
as.logical("FALSE")
as.logical("true")

6.2 逻辑值转数字 #

r
as.integer(TRUE)
as.integer(FALSE)

sum(c(TRUE, TRUE, FALSE))
mean(c(TRUE, TRUE, FALSE))

6.3 类型检查 #

r
x <- TRUE
is.logical(x)

y <- c(TRUE, FALSE)
is.logical(y)

七、缺失值处理 #

7.1 NA的逻辑运算 #

r
TRUE & NA
FALSE & NA
TRUE | NA
FALSE | NA
!NA

7.2 处理NA #

r
x <- c(TRUE, NA, FALSE)
all(x)
any(x)

all(x, na.rm = TRUE)
any(x, na.rm = TRUE)

7.3 检查NA #

r
x <- c(TRUE, NA, FALSE)
is.na(x)
!is.na(x)
x[!is.na(x)]

八、条件判断 #

8.1 if语句 #

r
x <- 10

if (x > 5) {
  print("x大于5")
}

8.2 if-else语句 #

r
x <- 3

if (x > 5) {
  print("x大于5")
} else {
  print("x不大于5")
}

8.3 多条件判断 #

r
score <- 75

if (score >= 90) {
  print("优秀")
} else if (score >= 80) {
  print("良好")
} else if (score >= 60) {
  print("及格")
} else {
  print("不及格")
}

8.4 向量化条件 #

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

ifelse(scores >= 90, "优秀",
       ifelse(scores >= 60, "及格", "不及格"))

九、逻辑索引 #

9.1 基本索引 #

r
x <- c(10, 20, 30, 40, 50)
x[x > 25]
x[x != 30]

9.2 组合条件 #

r
x <- c(10, 20, 30, 40, 50)
x[x > 20 & x < 50]
x[x < 20 | x > 40]
x[!(x == 30)]

9.3 数据框筛选 #

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

df[df$age > 24, ]
df[df$score >= 80, ]
df[df$age > 24 & df$score >= 80, ]

十、实践示例 #

10.1 成绩判断 #

r
grade <- function(score) {
  if (score >= 90) return("A")
  if (score >= 80) return("B")
  if (score >= 70) return("C")
  if (score >= 60) return("D")
  return("F")
}

grade(85)
grade(55)

10.2 数据筛选 #

r
data <- data.frame(
  id = 1:5,
  value = c(10, 25, 15, 30, 20)
)

valid_data <- data[data$value > 15, ]
print(valid_data)

10.3 条件计数 #

r
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

sum(x > 5)
sum(x %% 2 == 0)
mean(x > 5)

十一、总结 #

本章学习了:

  • 逻辑值的创建和表示
  • 逻辑运算符的使用
  • 比较运算和结果
  • 逻辑函数:all、any、which、ifelse
  • 类型转换方法
  • NA值的处理
  • 条件判断语句
  • 逻辑索引的应用

逻辑类型是控制程序流程的基础,掌握逻辑运算对于数据分析至关重要!

最后更新:2026-03-27