6.12 索引访问

which 与引用 [ 性能比较,在区间 [0,1] 上生成 10 万个服从均匀分布的随机数,随机抽取其中14

n <- 100000
x <- runif(n)
i <- logical(n)
i[sample(n, n / 4)] <- TRUE
microbenchmark::microbenchmark(x[i], x[which(i)], times = 1000)

使用 subset 函数与 [ 比较