- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 a data.table object in R by ignoring a value in one of the columns?
To subset a data.table object in R by ignoring a value in one of the columns, we can follow the below steps −
First of all, create a data.table object.
Then, use single square brackets to subset the data.table object by ignoring a value in one of the columns.
Example
Create the data.table object
Let’s create a data.table object as shown below −
library(data.table) x<-sample(1:3,25,replace=TRUE) y<-sample(1:4,25,replace=TRUE) z<-sample(1:3,25,replace=TRUE) DT<-data.table(x,y,z) DT
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1: 1 1 1 2: 1 3 1 3: 1 4 3 4: 3 3 1 5: 1 4 1 6: 1 2 3 7: 1 3 2 8: 1 3 1 9: 2 4 3 10: 1 4 3 11: 3 3 1 12: 3 2 2 13: 3 1 2 14: 2 4 2 15: 3 3 1 16: 3 2 1 17: 1 1 3 18: 2 4 1 19: 3 4 2 20: 3 2 1 21: 2 2 1 22: 3 4 1 23: 3 3 1 24: 1 1 1 25: 2 4 1 x y z
Subset the data.table object by ignoring a value in one of the columns
Using single square brackets to subset the data.table object DT by ignoring 4 in column y as shown below −
library(data.table) x<-sample(1:3,25,replace=TRUE) y<-sample(1:4,25,replace=TRUE) z<-sample(1:3,25,replace=TRUE) DT<-data.table(x,y,z) DT[DT$y!=4,]
Output
x y z 1: 1 1 1 2: 1 3 1 3: 3 3 1 4: 1 2 3 5: 1 3 2 6: 1 3 1 7: 3 3 1 8: 3 2 2 9: 3 1 2 10: 3 3 1 11: 3 2 1 12: 1 1 3 13: 3 2 1 14: 2 2 1 15: 3 3 1 16: 1 1 1
- Related Articles
- How to subset a matrix in R by ignoring a value in one of the columns?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to subset a data.table object in R by specifying columns that contains NA?
- How to subset a data.table in R by removing specific columns?
- How to subset a data.table object using a range of values in R?
- How to change data.table object columns value to maximum in R?
- How to combine two columns of a data.table object in R?
- How to divide data.table object rows by number of columns in R?
- How to standardize selected columns in data.table object in R?
- How to scale some columns in data.table object in R?
- How to randomize column values of a data.table object for all columns in R?
- How to randomize column values of a data.table object for a set of columns in R?
- How to multiply vector values in sequence with columns of a data.table object in R?
- How to standardize multiple columns not all in data.table object in R?
- How to delete columns by their name in data.table in R?

Advertisements