![]() |
VOOZH | about |
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:
Syntax:
dataset[condition]
Example:
mat[mat[,"car_color"]=="Red",]
Here, Comma(',') is used to return all the matrix rows.
Example:
Output: