Ian*_*ows 14
你可以通过消除base :: sample函数调用来获得一点加速:
> x<- rnorm(10000)
> system.time(for(i in 1:100000) x[.Internal(sample(10000L, 10L, FALSE, NULL))])
user system elapsed
2.873 0.017 2.851
> system.time(for(i in 1:100000) sample(x,10))
user system elapsed
3.420 0.025 3.258
Run Code Online (Sandbox Code Playgroud)
根据您的问题,可能还有其他更聪明的方法来加速您的代码.考虑用一个大的替换许多小样本的方法.