gpt4 book ai didi

r - 错误sample.int(长度(x),大小,替换,概率): invalid first argument

转载 作者:行者123 更新时间:2023-12-01 17:27:21 25 4
gpt4 key购买 nike

我正在运行以下代码行并面临上述错误。有什么想法可以解决这个问题吗?

X <- NULL
S <- NULL
remove.value <- NULL
N <- seq.int(1, 25)

repeat {
S <- sample(N, 1, replace = FALSE, prob = NULL)
S
if (S == 1) {
remove.value <- c(S, S + 1)
} else if (S == 25) {
remove.value <- c(S, S - 1)
} else {
remove.value <- c(S - 1, S, S + 1)
}
remove.value
N <- N [which(!(N %in% remove.value))]
N
if (is.null(N)) break
}

最佳答案

它实际上按照您的预期工作。如果插入 print(N)

就可以看到它
> repeat{

S<-sample(N ,1, replace = FALSE, prob = NULL)
S
if (S==1) {
remove.value<-c(S,S+1)
} else if (S==25) {
remove.value<-c(S,S-1)
}else {remove.value<-c(S-1,S,S+1)
}
remove.value

N <- N [which(N %!in% remove.value)]
print(N)

if (is.null(N)) break
}
[1] 1 2 3 4 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[1] 1 2 3 4 8 9 10 11 12 13 14 15 16 17 18 19 20 21 25
[1] 1 2 3 4 8 9 13 14 15 16 17 18 19 20 21 25
[1] 1 2 3 4 8 9 13 14 15 16 17 18 25
[1] 1 2 3 4 8 9 16 17 18 25
[1] 1 2 3 4 8 9 25
[1] 1 2 8 9 25
[1] 8 9 25
[1] 8 9
integer(0)
Error in sample.int(length(x), size, replace, prob) :
invalid first argument

该错误是由 N 的最终值为 integer(0) 而不是 NULL 引起的。如果您使用 if (length(N)== 0)break 而不是 if (is.null(N))break,则代码可以正常工作,不会出现错误消息。

关于r - 错误sample.int(长度(x),大小,替换,概率): invalid first argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204484/

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