为什么在对数据帧进行子集化时使用`which`会更快

时间:2015-01-20 05:15:45

标签: r dataframe subset

根据以下基准,which比不使用which快约5%。有谁知道为什么?

> microbenchmark(mtcars[mtcars$gear == 4 | mtcars$gear == 3,], times = 100000)
Unit: microseconds
                                          expr     min      lq     mean  median      uq      max neval
 mtcars[mtcars$gear == 4 | mtcars$gear == 3, ] 179.104 190.334 212.0044 194.876 199.662 85451.69 1e+05



> microbenchmark(mtcars[which(mtcars$gear == 4 | mtcars$gear == 3),],  times = 100000)
Unit: microseconds
                                                 expr     min      lq     mean  median      uq      max neval
 mtcars[which(mtcars$gear == 4 | mtcars$gear == 3), ] 168.744 179.569 196.0215 184.326 188.981 75434.62 1e+05

0 个答案:

没有答案