How to extract an element of a matrix by indexing based on column or row names? Explain

711    Asked by Daminidas in Data Science , Asked on Nov 12, 2019
Answered by Damini das

For extracting an element of a matrix,let us define a matrix along with row and column names

Matrix <- matrix(1:15, nrow=3)

rownames(Matrix) <- c("X", "Y","Z")

colnames(Matrix) <- c("A", "B", "C", "D", "E")

Matrix

 A B C D E

X 1 4 7 10 13

Y 2 5 8 11 14

Z 3 6 9 12 15

Now to extract an element of a matrix, we can do the following

Matrix["X", "A"]

[1] 1

Matrix["X", ]

Output

A B C D E

1 4 7 10 13



Your Answer

Interviews

Parent Categories