菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

R 矩阵


矩阵

矩阵是二维数据集,包含列和行。

列是数据的垂直表示,而行是数据的水平表示。

可以使用 matrix() 函数创建矩阵。指定 nrowncol 参数来设置行数和列数。

示例

# 创建一个矩阵
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)

# 打印矩阵
thismatrix
自己动手试一试 »

注意: 记住 c() 函数用于连接项。

您也可以使用字符串创建矩阵。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix
自己动手试一试 »

访问矩阵项

您可以使用 [ ] 方括号访问项。方括号中的第一个数字“1”指定行位置,第二个数字“2”指定列位置。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[1, 2]
自己动手试一试 »

如果您在括号中的数字后面指定逗号,则可以访问整行。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[2,]
自己动手试一试 »

如果您在括号中的数字前面指定逗号,则可以访问整列。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix[,2]
自己动手试一试 »


访问多行

如果使用 c() 函数,则可以访问多行。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

thismatrix[c(1,2),]
自己动手试一试 »

访问多列

如果使用 c() 函数,则可以访问多列。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

thismatrix[, c(1,2)]
自己动手试一试 »

添加行和列

使用 cbind() 函数在矩阵中添加其他列。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- cbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# 打印新矩阵
newmatrix
自己动手试一试 »

注意: 新列中的单元格必须与现有矩阵的长度相同。

使用 rbind() 函数在矩阵中添加其他行。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange","grape", "pineapple", "pear", "melon", "fig"), nrow = 3, ncol = 3)

newmatrix <- rbind(thismatrix, c("strawberry", "blueberry", "raspberry"))

# 打印新矩阵
newmatrix
自己动手试一试 »

注意: 新行中的单元格必须与现有矩阵的长度相同。


删除行和列

使用 c() 函数删除矩阵中的行和列。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"), nrow = 3, ncol =2)

# 删除第一行和第一列
thismatrix <- thismatrix[-c(1), -c(1)]

thismatrix
自己动手试一试 »

检查项是否存在

要查找指定的项是否在矩阵中,请使用 %in% 运算符。

示例

检查矩阵中是否存在“apple”

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

"apple" %in% thismatrix
自己动手试一试 »

行数和列数

使用 dim() 函数查找矩阵中的行数和列数。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

dim(thismatrix)
自己动手试一试 »

矩阵长度

使用 length() 函数查找矩阵的维度。

示例

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

length(thismatrix)
自己动手试一试 »

矩阵中的总单元格数是行数乘以列数。

在上面的示例中:维度 = 2*2 = **4**。


遍历矩阵

您可以使用 for 循环遍历矩阵。循环将从第一行开始,向右移动。

示例

循环遍历矩阵项并打印它们

thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

for (rows in 1:nrow(thismatrix)) {
  for (columns in 1:ncol(thismatrix)) {
    print(thismatrix[rows, columns])
  }
}
自己动手试一试 »

合并两个矩阵

同样,您可以使用 rbind()cbind() 函数将两个或多个矩阵合并在一起。

示例

# 合并矩阵
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)

# 按行添加
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined

# 按列添加
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
自己动手试一试 »

×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持