R 教程

浏览6141
更新时间2025-08-13

矩阵

矩阵是具有列和行的二维数据集。

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

可以使用 matrix() 函数创建矩阵。指定 nrow 和 ncol 参数来获取行数和列数:

实例

# 创建矩阵
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)

提示

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

在上面的示例中:Dimension = 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

相关视频

更多

免费

phpStudy极速入门视频教程
初级phpStudy极速入门视频教程

54.6万次学习

收藏

免费

phpStudy V8 视频教程
初级phpStudy V8 视频教程

38.8万次学习

收藏

免费

小皮面板使用视频教程
初级小皮面板使用视频教程

21.6万次学习

收藏

免费

好课诞生记
初级好课诞生记

7.1万次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

65万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

41.3万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

76.8万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.2万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.4万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.3万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

133.5万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载