按 R 中的列和行名称从矩阵中提取值
在本文中,我们将讨论如何在 R 编程语言中通过列名和行名从矩阵中提取值。
按列名从矩阵中提取值
可以使用所选行名称的过滤器从原始矩阵中提取行子集矩阵。由于矩阵的元素以双索引格式访问,因此可以执行特定的行选择。
句法:
matrix[ vec , ]
其中, vec 包含要获取的行名称
从数据框中检索所有列。行和列的顺序保持不变。提取后行和列名称保持不变。返回的结果是原始矩阵的子集。行名称应该是与矩阵有关的原始行名称的适当子集。
例子:
R
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting rows
row_vec <- c("R2","R4")
row_mat <- mat[row_vec ,]
print ("Modified Matrix")
print (row_mat)
R
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting rows
col_vec <- c("C1","C3")
col_mat <- mat[,col_vec]
print ("Modified Matrix")
print (col_mat)
R
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting rows
row_vec <- c("R1","R3")
# extracting columns
col_vec <- c("C1","C3")
col_mat <- mat[row_vec ,col_vec]
print ("Modified Matrix")
print (col_mat)
R
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting single element
col_mat <- mat["R3" , "C2"]
print ("Modified Matrix")
print (col_mat)
输出
[1] "Original Matrix"
C1 C2 C3
R1 "a" "e" "i"
R2 "b" "f" "j"
R3 "c" "g" "k"
R4 "d" "h" "l"
[1] "Modified Matrix"
C1 C2 C3
R2 "b" "f" "j"
R4 "d" "h" "l"
按行名称从矩阵中提取值
可以使用所选列名称的过滤器从原始矩阵中提取列子集矩阵。由于矩阵的元素以双索引格式访问,因此可以执行特定的行选择。
句法:
matrix[ , vec ]
其中, vec 包含要获取的列名
从数据框中检索所选列的所有行。行和列的顺序保持不变。提取后行和列名称保持不变。返回的结果是原始矩阵的子集。要选择的列名应该是与矩阵有关的原始行名的适当子集。
例子:
电阻
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting rows
col_vec <- c("C1","C3")
col_mat <- mat[,col_vec]
print ("Modified Matrix")
print (col_mat)
输出
[1] "Original Matrix"
C1 C2 C3
R1 "a" "e" "i"
R2 "b" "f" "j"
R3 "c" "g" "k"
R4 "d" "h" "l"
[1] "Modified Matrix"
C1 C3
R1 "a" "i"
R2 "b" "j"
R3 "c" "k"
R4 "d" "l"
使用列名和行名提取值
与上述方法类似,也可以通过为两个索引指定所选向量来提取行和列。
句法:
matrix[ rowvec , colvec ]
其中,rowvec 包含要获取的行名称,colvec 包含列名称
例子:
电阻
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting rows
row_vec <- c("R1","R3")
# extracting columns
col_vec <- c("C1","C3")
col_mat <- mat[row_vec ,col_vec]
print ("Modified Matrix")
print (col_mat)
输出
[1] "Original Matrix"
C1 C2 C3
R1 "a" "e" "i"
R2 "b" "f" "j"
R3 "c" "g" "k"
R4 "d" "h" "l"
[1] "Modified Matrix"
C1 C3
R1 "a" "i"
R3 "c" "k"
通过简单地将行名和列名指定为检索索引,也可以从矩阵中获取任何单个元素或单元格值。
例子:
电阻
# declaring matrix
mat <- matrix(letters[1:12], ncol = 3)
# naming columns
colnames(mat) <- c("C1","C2","C3")
# naming rows
rownames(mat) <- c("R1","R2","R3","R4")
print ("Original Matrix")
print (mat)
# extracting single element
col_mat <- mat["R3" , "C2"]
print ("Modified Matrix")
print (col_mat)
输出
[1] "Original Matrix"
C1 C2 C3
R1 "a" "e" "i"
R2 "b" "f" "j"
R3 "c" "g" "k"
R4 "d" "h" "l"
[1] "Modified Matrix"
[1] "k"