Open In App

Subset DataFrame and Matrix by Row Names in R

Last Updated : 26 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 to check for the presence of the data frame row names in the vector of required row names. The rows of the data frame are then retrieved from the data frame if they occur in the vector. The row names are returned in the final output of the data frame. The output is then used to return a subset of the data frame rows.

Syntax: val %in% vec

Arguments :

  • val – A list or vector of values to check in vector
  • vec – A vector to check the values in

Code:

R




# creating data frame
data_frame <- data.frame(col1 = rep(letters[1:4], each = 2),
                         col2 = 1:8
                         )
print("Original DataFrame")
print(data_frame)
  
# assigning row names to data frame
rownames(data_frame) <- c("row1","row2","row3","row4",
                          "row5", "row6","row7","row8")
  
# getting rows 
rows <- c("row1","row3","row5","row8")
  
# extracting data frame rows
data_mod <- data_frame[rownames(data_frame) %in% rows, ] 
print("Modified DataFrame")
print(data_mod)


Output:

Method 2: Subset matrix by row names 

The rownames(mat)method in R is used to set the names for rows of the matrix. A similar approach is used to check for the presence of row names of the matrix in the vector or list of specified row names. The following code snippet can be used to subset the matrix based on the specified row names.

Code:

R




# creating matrix
matr <- matrix(1:12, nrow = 4)
print("Original Matrix")
print(matr)
  
# assigning row names to data frame
rownames(matr) <- c("row1","row2","row3","row4")
                            
# getting rows 
rows <- c("row1","row3")
  
# extracting data frame rows
data_mod <- matr [rownames(matr) %in% rows, ] 
print("Modified Matrix")
print(data_mod)


Output:



Similar Reads

How to change Row Names of DataFrame in R ?
The rows are stacked together, each denoted by a unique name. By default, the integer identifiers beginning from 1 to the number of rows are assigned to the data frame by default. The task here is to change the Rows names in given dataframe using R programming. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : using rownames() A data fra
3 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all components. Every element is the same as each of the value
3 min read
Convert Row Names into Column of DataFrame in R
In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which contains the name of the column with the help of th
3 min read
Removing display of row names from dataframe in R
The dataframe rows and columns are referenced using unique row and column names for the elements. The dataframe method has an attribute row.names that don't make any modification to the existing structure of the dataframe, it just ignores the names assigned to rows. As a result, the first column consisting of row names is not displayed. By default,
3 min read
Convert Values in Column into Row Names of DataFrame in R
The row names in R are by default mapped to the row numbers, beginning with the integer value 1. The row names can be modified easily and reassigned to any possible string vector to assign customized names. Since, the row numbers are practically equal in each column of the dataframe, therefore the column values can also be assigned to the row names
4 min read
Substitute DataFrame Row Names by Values in Vector in R
In this article, we will discuss how to substitute dataframe row names by values in a vector in R programming language. Dataframe in use: We can substitute row names by using rownames() function Syntax: rownames(dataframe) &lt;- vector where, dataframe is the input dataframevector is the new row values Example: R program to substitute the rownames
2 min read
Extract Values from Matrix by Column and Row Names in R
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
4 min read
Create DataFrame Row by Row in R
In this article, we will discuss how to create dataframe row by row in R Programming Language. Method 1: Using for loop and indexing methods An empty data frame in R language can be created using the data.frame() method in R. For better clarity, the data types of the columns can be defined during the declaration. Each row of the data frame is a vec
4 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 allow a singular data type value to be stored acr
3 min read
Get or Set names of Elements of an Object in R Programming - names() Function
names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named. Syntax: names(x) &lt;- value Para
2 min read