Skip to content
Related Articles

Related Articles

Select rows of a matrix in R that meet a condition

View Discussion
Improve Article
Save Article
Like Article
  • 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:

             car_models         car_type       car_color       year   

1

Maruti

Diesel

Red

2001

2

Hyundai

Petrol

Blue

2011

3

Tata

Petrol

Red

2013

4

Ford

Diesel

Red

2012

5

Nissan

Petrol

Blue

2021

6

Toyota

Diesel

Red

2021

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:

            

    car_models   

    car_type   

    car_color   

   year  

1

Maruti

Diesel

Red

2001

2

Tata

Petrol

Red

2013

3

Ford

Diesel

Red

2012

4

Toyota

Diesel

Red

2021

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
Recommended Articles
Page :

Start Your Coding Journey Now!