How to subset a data.table in R by removing specific columns?


After getting some experience with data frame people generally move on to data.table object because it is easy to play with a data.table object as compared to a data frame. We also need to create a subset of a data.table object and it can be easily done with the help of square brackets. For example, if we have a data.table object called DT that contains 10 columns and we want to create a subset of 1 to 8 columns then we can use DT[,−c(9,10),with=FALSE] to create that subset.

Example

library(data.table)
x1<−rnorm(20,1,0.5)
x2<−rnorm(20,1,0.45)
x3<−rpois(20,10)
x4<−rpois(20,5)
x5<−rpois(20,8)
x6<−rexp(20,1.15)
x7<−sample(0:9,20,replace=TRUE)
x8<−sample(41:49,20,replace=TRUE)
DT<−data.table(x1,x2,x3,x4,x5,x6,x7,x8)
DT

Output

x1 x2 x3 x4 x5 x6 x7 x8
1: 0.3816564 0.8224154 11 5 9 0.02823792 6 44
2: 1.2498355 1.3669585 9 5 15 1.41967863 8 45
3: 1.7661182 1.1508072 14 8 3 0.99296213 7 44
4: 0.4679560 1.9738294 9 6 8 1.30810100 0 46
5: 1.2634799 1.7418108 8 2 10 1.57067044 0 44
6: 0.5271280 0.7788231 15 4 9 0.85467421 3 42
7: 1.1978085 1.1891952 10 4 9 0.76186350 9 44
8: 0.5582059 1.2431360 12 4 7 1.60622936 0 43
9: 0.5003223 0.9426929 11 8 13 1.86129287 2 46
10: 0.5168766 0.8995718 8 3 7 0.66378569 6 48
11: 1.2805334 1.0410280 9 2 7 0.78355511 0 48
12: 1.5555755 1.2342420 10 7 2 3.99523338 6 45
13: 1.0936011 2.1838511 12 10 7 0.31020687 5 48
14: 0.8434001 0.6962775 19 5 8 1.12643977 8 41
15: 0.4765863 1.1157518 8 6 9 0.72010177 4 45
16: 0.9317724 0.8178368 6 6 7 0.55444159 4 48
17: 1.5397574 1.2234017 11 4 6 0.29338357 3 41
18: 0.7530927 1.4972635 8 8 12 0.27459776 9 43
19: 1.3672898 1.6516958 14 6 7 2.99439984 0 48
20: 0.3179253 0.5976030 7 5 7 0.55743554 8 44

Subsetting all columns except 1 and 2 −

DT[,−c(1,2),with=FALSE]

Output

x3 x4 x5 x6 x7 x8
1: 11 5 9 0.02823792 6 44
2: 9 5 15 1.41967863 8 45
3: 14 8 3 0.99296213 7 44
4: 9 6 8 1.30810100 0 46
5: 8 2 10 1.57067044 0 44
6: 15 4 9 0.85467421 3 42
7: 10 4 9 0.76186350 9 44
8: 12 4 7 1.60622936 0 43
9: 11 8 13 1.86129287 2 46
10: 8 3 7 0.66378569 6 48
11: 9 2 7 0.78355511 0 48
12: 10 7 2 3.99523338 6 45
13: 12 10 7 0.31020687 5 48
14: 19 5 8 1.12643977 8 41
15: 8 6 9 0.72010177 4 45
16: 6 6 7 0.55444159 4 48
17: 11 4 6 0.29338357 3 41
18: 8 8 12 0.27459776 9 43
19: 14 6 7 2.99439984 0 48
20: 7 5 7 0.55743554 8 44

Subsetting all columns except 1 and 8 −

DT[,−c(1,8),with=FALSE]

Output

x2 x3 x4 x5 x6 x7
1: 0.8224154 11 5 9 0.02823792 6
2: 1.3669585 9 5 15 1.41967863 8
3: 1.1508072 14 8 3 0.99296213 7
4: 1.9738294 9 6 8 1.30810100 0
5: 1.7418108 8 2 10 1.57067044 0
6: 0.7788231 15 4 9 0.85467421 3
7: 1.1891952 10 4 9 0.76186350 9
8: 1.2431360 12 4 7 1.60622936 0
9: 0.9426929 11 8 13 1.86129287 2
10: 0.8995718 8 3 7 0.66378569 6
11: 1.0410280 9 2 7 0.78355511 0
12: 1.2342420 10 7 2 3.99523338 6
13: 2.1838511 12 10 7 0.31020687 5
14: 0.6962775 19 5 8 1.12643977 8
15: 1.1157518 8 6 9 0.72010177 4
16: 0.8178368 6 6 7 0.55444159 4
17: 1.2234017 11 4 6 0.29338357 3
18: 1.4972635 8 8 12 0.27459776 9
19: 1.6516958 14 6 7 2.99439984 0
20: 0.5976030 7 5 7 0.55743554 8

Subsetting all columns except 5 to 8 −

DT[,−c(5:8),with=FALSE]

Output

x1 x2 x3 x4
1: 0.3816564 0.8224154 11 5
2: 1.2498355 1.3669585 9 5
3: 1.7661182 1.1508072 14 8
4: 0.4679560 1.9738294 9 6
5: 1.2634799 1.7418108 8 2
6: 0.5271280 0.7788231 15 4
7: 1.1978085 1.1891952 10 4
8: 0.5582059 1.2431360 12 4
9: 0.5003223 0.9426929 11 8
10: 0.5168766 0.8995718 8 3
11: 1.2805334 1.0410280 9 2
12: 1.5555755 1.2342420 10 7
13: 1.0936011 2.1838511 12 10
14: 0.8434001 0.6962775 19 5
15: 0.4765863 1.1157518 8 6
16: 0.9317724 0.8178368 6 6
17: 1.5397574 1.2234017 11 4
18: 0.7530927 1.4972635 8 8
19: 1.3672898 1.6516958 14 6
20: 0.3179253 0.5976030 7 5

Subsetting all columns except 2, 4, 7, and 8 −

DT[,−c(2,4,7,8),with=FALSE]

Output

x1 x3 x5 x6
1: 0.3816564 11 9 0.02823792
2: 1.2498355 9 15 1.41967863
3: 1.7661182 14 3 0.99296213
4: 0.4679560 9 8 1.30810100
5: 1.2634799 8 10 1.57067044
6: 0.5271280 15 9 0.85467421
7: 1.1978085 10 9 0.76186350
8: 0.5582059 12 7 1.60622936
9: 0.5003223 11 13 1.86129287
10: 0.5168766 8 7 0.66378569
11: 1.2805334 9 7 0.78355511
12: 1.5555755 10 2 3.99523338
13: 1.0936011 12 7 0.31020687
14: 0.8434001 19 8 1.12643977
15: 0.4765863 8 9 0.72010177
16: 0.9317724 6 7 0.55444159
17: 1.5397574 11 6 0.29338357
18: 0.7530927 8 12 0.27459776
19: 1.3672898 14 7 2.99439984
20: 0.3179253 7 7 0.55743554

Updated on: 06-Nov-2020

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements