gpt4 book ai didi

r - 为什么sample()不适用于单个数字?

转载 作者:行者123 更新时间:2023-12-02 09:07:18 24 4
gpt4 key购买 nike

sample(x,n)参数是向量,以及您希望采样的次数

sample(c(5,9),1) returns either 5 or 9

但是,

sample(5,1) returns 1,2,3,4, or 5?

我已阅读帮助部分:

If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x). See the examples.

但是有没有办法让它不这样做呢?或者我是否只需要包含一个 if 语句来避免这种情况。

最佳答案

Or do I just need to include an if statement to avoid this.

是的,不幸的是。像这样的事情:

result = if(length(x) == 1) {x} else {sample(x, ...)}

关于r - 为什么sample()不适用于单个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56549778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com