How to subset a data.table object using a range of values in R?



To subset a data.table object using a range of values, we can use single square brackets and choose the range using %between%. For example, if we have a data.table object DT that contains a column x and the values in x ranges from 1 to 10 then we can subset DT for values between 3 to 8 by using the command DT[DT$x %between% c(3,8)].

Example1

Loading data.table package and creating a data.table object −

library(data.table) x1<-rnorm(20) x2<-rnorm(20,100,3.25) dt1<-data.table(x1,x2) dt1

Output

       x1         x2 1:   0.06546822   102.83348 2:   1.05756760   99.28015 3:   0.09397791   97.36582 4:  -0.44478256   96.22510 5:  -0.17392089   99.56077 6:   0.32834773   95.85831 7:  -0.04563975   104.88298 8:   0.95807930   95.31325 9:   1.25349243   101.72948 10:  0.47676550   96.76459 11: -1.26790256   98.76222 12: -1.36220203   97.91117 13: -1.31999499   102.81730 14:  1.69391374   96.00380 15: -0.10801512   101.69544 16: -1.13463486   108.11833 17: -0.13885823   102.34798 18: -0.54403332   99.68874 19:  0.55865944   97.33505 20:  0.76511431   96.53975

Subsetting rows of dt1 for values 0.5 to 1 of x1 −

dt1[dt1$x1 %between% c(0.5,1)]

      x1         x2 1: 0.9580793  95.31325 2: 0.5586594  97.33505 3: 0.7651143  96.53975

Example2

y1<-rpois(20,5) y2<-rpois(20,2) dt2<-data.table(y1,y2) dt2

Output

    y1 y2 1:  7  1 2:  3  2 3:  1  5 4:  4  2 5:  5  3 6:  4  1 7:  3  2 8:  5  0 9:  5  2 10: 7  4 11: 5  4 12: 6  2 13: 6  4 14: 6  2 15: 5  2 16: 1  1 17: 3  1 18: 2  4 19: 4  0 20: 4  1

Subsetting rows of dt1 for values 2 to 6 of y2 −

dt2[dt2$y2 %between% c(2,6)]

    y1 y2 1:  3  2 2:  1  5 3:  4  2 4:  5  3 5:  3  2 6:  5  2 7:  7  4 8:  5  4 9:  6  2 10: 6  4 11: 6  2 12: 5  2 13: 2  4
Updated on: 2021-03-06T12:54:18+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements