矩阵是具有列和行的二维数据集。
列是数据的垂直表示,而行是数据的水平表示。
可以使用以下命令创建矩阵matrix()
功能。指定nrow
和ncol
获取行数和列数的参数:
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
# Print the matrix
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"))
# Print the new matrix
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"))
# Print the new matrix
newmatrix
亲自试一试 »
笔记:新行中的单元格必须与现有矩阵的长度相同。
使用c()
删除矩阵中的行和列的函数:
thismatrix <- matrix(c("apple", "banana", "cherry", "orange", "mango", "pineapple"), nrow = 3, ncol =2)
#Remove the first row and the first column
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()
将两个或多个矩阵组合在一起的函数:
# Combine matrices
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol = 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow = 2, ncol = 2)
# Adding it as a rows
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
# Adding it as a columns
Matrix_Combined <- cbind(Matrix1, Matrix2)
Matrix_Combined
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!