Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 remove rows in data.table object in R that contains a specific number?
To remove rows in data.table object in R that contains a specific number, we can follow the below steps −
First of all, create a data.table object.
Then, use single square subsetting with apply function to remove rows that contains a specific number.
Example
Create the data.table object
Let’s create a data.table object as shown below: −
library(data.table) x<-sample(1:25,25) y<-sample(1:25,25) z<-sample(1:25,25) 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: 24 3 24
2: 23 5 11
3: 2 2 8
4: 10 4 9
5: 21 12 20
6: 4 18 16
7: 11 19 19
8: 6 21 17
9: 9 23 23
10: 8 6 2
11: 20 7 15
12: 7 22 22
13: 13 17 4
14: 15 14 13
15: 25 16 14
16: 16 9 12
17: 1 24 5
18: 3 11 10
19: 19 20 25
20: 22 10 3
21: 14 25 7
22: 12 8 1
23: 18 1 18
24: 17 15 21
25: 5 13 6
x y z
Remove rows that contains a specific number
Using single square subsetting with apply function to remove rows that contains 10 in data.table object DT −
library(data.table) x<-sample(1:25,25) y<-sample(1:25,25) z<-sample(1:25,25) DT<-data.table(x,y,z) DT[!apply(DT==10,1,any),]
Output
x y z
1: 24 3 24
2: 23 5 11
3: 2 2 8
4: 21 12 20
5: 4 18 16
6: 11 19 19
7: 6 21 17
8: 9 23 23
9: 8 6 2
10: 20 7 15
11: 7 22 22
12: 13 17 4
13: 15 14 13
14: 25 16 14
15: 16 9 12
16: 1 24 5
17: 19 20 25
18: 14 25 7
19: 12 8 1
20: 18 1 18
21: 17 15 21
22: 5 13 6
x y zAdvertisements