sample函数注意事项

# sample()'s surprise -- example
x <- 1:10
    sample(x[x >  8]) # length 2
    sample(x[x >  9]) # oops -- length 10!#意思就是,当选择的子变量是一个大于零的整数时,比如sample(10),就会返回1:10中的随机排列了,等同于sample(1:10,10,replace=FALSE)
    sample(x[x > 10]) # length 0

## safer version:
resample <- function(x, ...) x[sample.int(length(x), ...)]
resample(x[x >  8]) # length 2
resample(x[x >  9]) # length 1
resample(x[x > 10]) # length 0

Guess you like

Origin blog.csdn.net/weixin_42683052/article/details/109569152