R 矩阵

矩阵是一种二维数据结构,数据排列成行和列。例如:

A matrix arrange data into rows and columns
R 3 * 3 矩阵

这里,上面的矩阵是一个 3 * 3(读作“三乘三”)矩阵,因为它有 3 行和 3 列。


在 R 中创建矩阵

在 R 中,我们使用 matrix() 函数来创建矩阵。

matrix() 函数的语法是

matrix(vector, nrow, ncol)

这里,

  • vector - 相同类型的数据项
  • nrow - 行数
  • ncol - 列数
  • byrow (可选) - 如果为 TRUE,则矩阵按行填充。默认情况下,矩阵按列填充。

让我们看一个例子,

# create a 2 by 3 matrix
matrix1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)

print(matrix1)

输出

    [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

在上面的示例中,我们使用了 matrix() 函数来创建一个名为 matrix1 的矩阵。

matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)

在这里,我们传递了整数类型的数据项,并使用 c() 将数据项组合在一起。nrow = 2ncol = 3 表示矩阵有 2 行和 3 列。

由于我们传递了 byrow = TRUE,矩阵中的数据项按行填充。如果我们没有传递 byrow 参数,如下:

matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)

输出将是

    [,1]  [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

在 R 中访问矩阵元素

我们使用向量索引运算符 [ ] 来访问 R 矩阵中的特定元素。

访问矩阵元素的语法是

matrix[n1, n2]

这里,

  • n1 - 指定行位置
  • n2 - 指定列位置

让我们看一个例子,

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol = 2)

print(matrix1)

# access element at 1st row, 2nd column
cat("\nDesired Element:", matrix1[1, 2])

输出

      [,1]          [,2]   
[1,] "Sabby" "Larry"
[2,] "Cathy" "Harry"

Desired Element: Larry

在上面的示例中,我们创建了一个名为 matrix122 列矩阵,包含 4 个字符串类型数据。注意索引运算符 [] 的使用,

matrix1[1, 2]

这里,[1, 2] 指定我们试图访问位于 第 1 行,第 2 列的元素,即 "Larry"

访问整行或整列

在 R 中,我们还可以根据 [] 中传递的值访问整行或整列。

  • [n, ] - 返回 第 n 行的所有元素。
  • [ ,n] - 返回 第 n 列的所有元素。

例如,

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol = 2)

print(matrix1)

# access entire element at 1st row
cat("\n1st Row:", matrix1[1, ])

# access entire element at 2nd column
cat("\n2nd Column:", matrix1[, 2])

输出

       [,1]         [,2]   
[1,] "Sabby" "Larry"
[2,] "Cathy" "Harry"

1st Row: Sabby Larry
2nd Column: Larry Harry

这里,

  • matrix1[1, ] - 访问 第 1 行的所有元素,即 SabbyLarry
  • matrix1[ ,2] - 访问 第 2 列的所有元素,即 LarryHarry

访问多行或多列

我们可以使用 c() 函数在 R 中访问多行或多列。

  • [c(n1,n2), ] - 返回 n1n2 行的所有元素。
  • [ ,c(n1,n2)] - 返回 n1n2 列的所有元素。

例如,

# create 2 by 3 matrix
matrix1 <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 2, ncol = 3)

print(matrix1)

# access entire element of 1st and 3rd row
cat("\n1st and 2nd Row:", matrix1[c(1,3), ])

# access entire element of 2nd and 3rd column
cat("\n2nd and 3rd Column:", matrix1[  ,c(2,3)])

输出

       [,1] [,2]  [,3]
[1,]   10   30   50
[2,]   20   40   60

1st and 3rd Row: 10 20 30 40 50 60
2nd and 3rd Column: 30 40 50 60

这里,

  • [c(1,3), ] - 返回 第 1 行和 第 3 行的所有元素。
  • [ ,c(2,3)] - 返回 第 2 列和 第 3 列的所有元素。

在 R 中修改矩阵元素

我们使用向量索引运算符 [] 来修改指定的元素。例如:

matrix1[1,2] = 140

这里,位于 第 1 行、第 2 列的元素被更改为 140

让我们看一个例子,

# create 2 by 2 matrix
matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)

# print original matrix
print(matrix1)

# change value at 1st row, 2nd column to 5 
matrix1[1,2] = 5

# print updated matrix
print(matrix1)

输出

    [,1] [,2]
[1,]    1    3
[2,]    2    4

     [,1] [,2]
[1,]    1    5
[2,]    2    4

在 R 中组合两个矩阵

在 R 中,我们使用 cbind()rbind() 函数将两个矩阵组合在一起。

  • cbind() - 按列组合两个矩阵
  • rbind() - 按行组合两个矩阵

我们要组合的两个矩阵的行数和列数必须相等。例如:

# create two 2 by 2 matrices 
even_numbers <- matrix(c(2, 4, 6, 8), nrow = 2, ncol = 2)
odd_numbers <- matrix(c(1, 3, 5, 7), nrow = 2, ncol = 2)

# combine two matrices by column
total1 <- cbind(even_numbers, odd_numbers)
print(total1)

# combine two matrices by row
total2 <- rbind(even_numbers, odd_numbers)
print(total2)

输出

    [,1] [,2] [,3] [,4]
[1,]    2    6    1    5
[2,]    4    8    3    7
     [,1] [,2]
[1,]    2    6
[2,]    4    8
[3,]    1    5
[4,]    3    7

这里,我们首先使用 cbind() 函数按列组合了两个矩阵:even_numbersodd_numbers。然后使用 rbind() 按行组合两个矩阵。


检查 R 矩阵中是否存在元素

在 R 中,我们使用 %in% 运算符来检查指定元素是否存在于矩阵中,并返回一个布尔值。

  • TRUE - 如果指定元素存在于矩阵中
  • FALSE - 如果指定元素不存在于矩阵中

例如,

matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol = 2)

"Larry" %in% matrix1 # TRUE

"Kinsley" %in% matrix1 # FALSE

输出

TRUE
FALSE

这里,

  • "Larry" 存在于 matrix1 中,因此该方法返回 TRUE
  • "Kinsley" 不存在于 matrix1 中,因此该方法返回 FALSE

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战