- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to subset an R data frame by ignoring a value in one of the columns?
To subset an R data frame by ignoring a value in one of the columns, we can follow the below steps −
First of all, create a data frame.
Then, use single square brackets to subset the data frame by ignoring a value in one of the columns.
Example
Create the data frame
Let’s create a data frame as shown below −
x<-rpois(30,5) y<-rpois(30,5) z<-rpois(30,2) df<-data.frame(x,y,z) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1 2 10 3 2 5 3 2 3 2 5 0 4 5 5 1 5 5 6 1 6 4 8 1 7 4 7 5 8 7 7 2 9 5 4 0 10 7 6 2 11 4 2 2 12 6 4 4 13 9 2 1 14 9 2 1 15 4 1 2 16 8 3 1 17 4 3 1 18 6 5 5 19 3 4 2 20 3 8 2 21 6 1 4 22 6 11 1 23 5 6 2 24 4 5 0 25 3 7 2 26 6 5 1 27 6 6 0 28 2 5 2 29 5 3 1 30 5 4 2
Subset the data frame by ignoring a value in one of the columns
Using single square brackets to subset the data frame df by ignoring 2 in column z as shown below −
x<-rpois(30,5) y<-rpois(30,5) z<-rpois(30,2) df<-data.frame(x,y,z) df[df$z!=2,]
Output
x y z 1 2 10 3 3 2 5 0 4 5 5 1 5 5 6 1 6 4 8 1 7 4 7 5 9 5 4 0 12 6 4 4 13 9 2 1 14 9 2 1 16 8 3 1 17 4 3 1 18 6 5 5 21 6 1 4 22 6 11 1 24 4 5 0 26 6 5 1 27 6 6 0 29 5 3 1
- Related Articles
- How to subset a matrix in R by ignoring a value in one of the columns?
- How to subset a data.table object in R by ignoring a value in one of the columns?
- How to subset factor columns in an R data frame?
- How to subset a data frame by excluding a specific text value in an R data frame?
- How to subset an R data frame by specifying columns that contains NA?
- How to find the row mean for columns in an R data frame by ignoring missing values?
- How to create a subset of an R data frame based on multiple columns?
- Combine two columns by ignoring missing values if exists in one column in R data frame.
- How to subset rows of an R data frame if any columns have values greater than a certain value?
- How to subset rows of an R data frame if all columns have values greater than a certain value
- How to subset rows that do not contain NA and blank in one of the columns in an R data frame?
- How to subset columns that has less than four categories in an R data frame?
- How to subset an R data frame based on string values of a columns with OR condition?
- How to filter rows by excluding a particular value in columns of the R data frame?
- How to replace NA’s to a value of selected columns in an R data frame?

Advertisements