Extract Values from Matrix by Column and Row Names in R
Last Updated :
23 May, 2021
In this article, we will discuss how to extract values from the matrix by column and row names in R Programming Language.
Extracting values from matrix by Column names
A row subset matrix can be extracted from the original matrix using a filter for the selected row names. Since a matrix's elements are accessed in a dual index format, particular row selection can be carried out.
Syntax:
matrix[ vec , ]
Where, vec contains the row names to be fetched
All the columns are retrieved from the data frame. The order of the rows and columns remains unmodified. The rows and column names remain unchanged after extraction. The result returned is a subset of the original matrix. The row names should be a proper subset of the original row names pertaining to matrix.
Example:
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)
Output
[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"
Extracting values from matrix by row names
A column subset matrix can be extracted from the original matrix using a filter for the selected column names. Since a matrix's elements are accessed in a dual index format, particular row selection can be carried out.
Syntax:
matrix[ , vec ]
Where, vec contains the column names to be fetched
All the rows for the selected columns are retrieved from the data frame. The order of the rows and columns remains unmodified. The rows and column names remain unchanged after extraction. The result returned is a subset of the original matrix. The column names to be chosen should be a proper subset of the original row names pertaining to the matrix.
Example:
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)
Output
[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"
Extracting values using both column and row names
Similar to the above approaches, the rows, and columns can also be extracted, by specifying the chosen vectors for both indexes.
Syntax:
matrix[ rowvec , colvec ]
Where, rowvec contains the row names to be fetched and colvec the column names
Example:
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)
Output
[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"
Any single element or cell value can also be fetched from the matrix, by simply specifying the row name and column name as the indexes for retrieval.
Example:
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)
Output
[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"
Similar Reads
Subset DataFrame and Matrix by Row Names in R In this article, we are going to see how to evaluate subset dataframe and matrices by row name. Method 1: Subset dataframe by row names The rownames(df) method in R is used to set the names for rows of the data frame. A vector of the required row names is specified. The %in% operator in R is used t
2 min read
Convert DataFrame to Matrix with Column Names in R Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly
3 min read
Extract given rows and columns from a given dataframe in R Extraction of given rows and columns has always been one of the most important tasks which are especially required while working on data cleaning activities. In this article, we will be discussing all the sets of commands which are used to extract given rows and columns from a given dataframe in the
4 min read
Drop column(s) by name from a given DataFrame in R Dropping of columns from a data frame is simply used to remove the unwanted columns in the data frame. In this article, we will be discussing the two different approaches to drop columns by name from a given Data Frame in R. The different approaches to drop columns by the name from a data frame is R
3 min read
Extract column from list in R In this article, we are going to create a list of elements and access those columns in R. We are first creating a list with matrix and vectors and access those columns using R. Approach Create a list Syntax: list_name=list(var1, var2, varn..) Assign names to list elements as columns names. We can gi
3 min read