要data.table使用一定范围的值对对象进行子集设置,可以使用单个方括号,然后使用%between%选择范围。例如,如果我们有一个data.table对象DT包含x列,并且x中的值介于1到10之间,则可以使用命令DT [DT $x%between%c(3 ,8)]。
加载data.table包并创建一个data.table对象-
library(data.table) x1<-rnorm(20) x2<-rnorm(20,100,3.25) dt1<-data.table(x1,x2) dt1输出结果
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
为x1的值0.5到1设置dt1的行-
dt1 [dt1 $x1%c(0.5,1)%之间]
x1 x2 1: 0.9580793 95.31325 2: 0.5586594 97.33505 3: 0.7651143 96.53975
y1<-rpois(20,5) y2<-rpois(20,2) dt2<-data.table(y1,y2) dt2输出结果
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
为y2的值2到6设置dt1的行-
dt2 [dt2 $y2%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