# Select rows of a matrix in R that meet a condition

• Last Updated : 26 Sep, 2021

A large dataset is often required to be filtered according to our requirements. In this article, we will be discussing how we can select a row from a matrix in R that meets the condition. For better understanding let’s understand the problem statement with the help of an example.

Example:

Data in use:

Now, as the problem statement is that we want to select the rows of the matrix that meets the given condition. Suppose we want to select the rows from the matrix whose car_color = Red.

Then, the output must look like this:

Approach:

• Create dataset
• Specify the condition
• Pass it to the matrix
• Select rows which specify this condition

Syntax:

dataset[condition]

Example:

mat[mat[,”car_color”]==”Red”,]

Here, Comma(‘,’) is used to return all the matrix rows.

• Copy the resultant dataset to auxiliary dataset
• Display dataset

Example:

## R

 `# Creating Dataset` `car_models <- ``c``(``'Maruti'``,``'Hyundai'``,``'Tata'``,``                ``'Ford'``,``'Nissan'``,``'Toyota'``)` `car_type <- ``c``(``'Diesel'``,``'Petrol'``,``'Petrol'``,``              ``'Diesel'``,``'Petrol'``,``'Diesel'``)` `car_color <- ``c``(``'Red'``,``'Blue'``,``'Red'``,``               ``'Red'``,``'Blue'``,``'Red'``)` `year <- ``c``(2001,2011,2013,2012,2021,2021)` `# Storing matrix in mat (variable)``mat <- ``cbind``(car_models,car_type,car_color,year)` `# condition to select only rows with``# color = Red``mat <- mat[mat[,``"car_color"``]==``"Red"``,]` `# displaying the resultant matrix``mat`

Output:

Resultant Matrix

My Personal Notes arrow_drop_up