Subset DataFrame and Matrix by Row Names in R Last Updated : 26 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like 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 vectorvec - 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: Comment More infoAdvertise with us Next Article Subset DataFrame and Matrix by Row Names in R M mallikagupta90 Follow Improve Article Tags : R Language R Programs R-Matrix R-DataFrame R DataFrame-Programs R Matrix-Programs +2 More Similar Reads Merge DataFrames by Row Names in R In this article, we are going to see how to merge Dataframe by Row Name using merge in R Programming Language. The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to 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 a 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) <- vector where, dataframe is the input dataframevector is the new row val 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 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 SecondThird1a72a 3 min read Like