R语言向量 #

一、向量概述 #

向量(Vector)是R语言中最基本的数据结构。向量是相同类型元素的有序集合,所有元素必须是同一类型。

二、创建向量 #

2.1 使用c函数 #

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

y <- c("a", "b", "c")
print(y)
class(y)

z <- c(TRUE, FALSE, TRUE)
print(z)
class(z)

2.2 使用冒号运算符 #

r
x <- 1:5
print(x)

y <- 5:1
print(y)

z <- -3:3
print(z)

2.3 使用seq函数 #

r
x <- seq(1, 10)
print(x)

y <- seq(1, 10, by = 2)
print(y)

z <- seq(1, 10, length.out = 5)
print(z)

w <- seq(0, 1, by = 0.1)
print(w)

2.4 使用rep函数 #

r
x <- rep(1, times = 5)
print(x)

y <- rep(1:3, times = 2)
print(y)

z <- rep(1:3, each = 2)
print(z)

w <- rep(c("a", "b"), times = c(2, 3))
print(w)

2.5 组合向量 #

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

三、向量属性 #

3.1 长度 #

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

3.2 类型 #

r
x <- c(1, 2, 3)
class(x)
typeof(x)
mode(x)

3.3 名称 #

r
x <- c(10, 20, 30)
names(x) <- c("a", "b", "c")
print(x)

y <- c(a = 10, b = 20, c = 30)
print(y)
names(y)

四、向量索引 #

4.1 正整数索引 #

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

x[1]
x[c(1, 3, 5)]
x[1:3]

4.2 负整数索引 #

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

x[-1]
x[-c(1, 2)]
x[-(1:2)]

4.3 逻辑索引 #

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

x[x > 25]
x[x != 30]
x[x >= 30 & x <= 50]

4.4 名称索引 #

r
x <- c(a = 10, b = 20, c = 30)

x["a"]
x[c("a", "c")]

4.5 空索引 #

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

x[]
x[0]
x[6]

五、向量运算 #

5.1 算术运算 #

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

x + y
x - y
x * y
x / y
x ^ 2

5.2 与标量运算 #

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

x + 10
x * 2
x / 2
x ^ 2

5.3 循环补齐 #

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

x + y

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

x + y

5.4 比较运算 #

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

x > 3
x == 3
x != 3
x >= 2 & x <= 4

5.5 数学函数 #

r
x <- c(-2, -1, 0, 1, 2)

abs(x)
sqrt(abs(x))
log(abs(x) + 1)
exp(x)

六、向量函数 #

6.1 统计函数 #

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

sum(x)
mean(x)
median(x)
sd(x)
var(x)

6.2 范围函数 #

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

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

6.3 排序函数 #

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

sort(x)
sort(x, decreasing = TRUE)
order(x)
rank(x)
rev(x)

6.4 累积函数 #

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

cumsum(x)
cumprod(x)
cummax(x)
cummin(x)

6.5 查找函数 #

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

which(x == 30)
which(x > 25)
which.max(x)
which.min(x)

6.6 唯一值 #

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

unique(x)
duplicated(x)
x[!duplicated(x)]

七、向量修改 #

7.1 修改元素 #

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

x[1] <- 10
print(x)

x[c(1, 3)] <- c(100, 300)
print(x)

7.2 添加元素 #

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

x <- c(x, 4)
print(x)

x <- c(0, x)
print(x)

append(x, 10, after = 2)

7.3 删除元素 #

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

x <- x[-1]
print(x)

x <- x[x != 3]
print(x)

八、向量类型 #

8.1 类型强制转换 #

r
x <- c(1, 2, "a")
class(x)

y <- c(TRUE, 1, 2)
class(y)

z <- c(TRUE, FALSE, "TRUE")
class(z)

8.2 类型检查 #

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

is.numeric(x)
is.character(x)
is.logical(x)
is.vector(x)

8.3 类型转换 #

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

as.character(x)
as.logical(x)

y <- c("1", "2", "3")
as.numeric(y)

九、特殊向量 #

9.1 空向量 #

r
x <- c()
x <- vector()
x <- numeric(0)
x <- character(0)

length(x)

9.2 NA向量 #

r
x <- c(1, NA, 3, NA, 5)
is.na(x)
x[!is.na(x)]
sum(x, na.rm = TRUE)

9.3 常量向量 #

r
LETTERS
letters
month.name
month.abb
pi

十、集合操作 #

10.1 并集 #

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

union(x, y)

10.2 交集 #

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

intersect(x, y)

10.3 差集 #

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

setdiff(x, y)
setdiff(y, x)

10.4 集合相等 #

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

setequal(x, y)

10.5 成员判断 #

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

1 %in% x
c(1, 6) %in% x

十一、实践示例 #

11.1 成绩分析 #

r
scores <- c(85, 92, 78, 90, 88, 76, 95, 82)

cat("平均分:", mean(scores), "\n")
cat("最高分:", max(scores), "\n")
cat("最低分:", min(scores), "\n")
cat("标准差:", round(sd(scores), 2), "\n")
cat("及格人数:", sum(scores >= 60), "\n")

11.2 数据筛选 #

r
data <- c(10, 25, 15, 30, 20, 35, 25, 40)

above_20 <- data[data > 20]
print(above_20)

between <- data[data >= 20 & data <= 30]
print(between)

11.3 向量标准化 #

r
normalize <- function(x) {
  (x - min(x)) / (max(x) - min(x))
}

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

十二、总结 #

本章学习了:

  • 向量的多种创建方法
  • 向量属性:长度、类型、名称
  • 向量索引:正负整数、逻辑、名称
  • 向量运算和循环补齐
  • 常用向量函数
  • 向量修改操作
  • 类型转换和检查
  • 集合操作

向量是R语言的核心数据结构,掌握向量操作是学习R语言的基础!

最后更新:2026-03-27