我运行下面的代码行,面向上述错误。任何想法如何解决这一问题 ?
install.packages("operator.tools")
X <- NULL
S <- NULL
remove.valuex <- NULL
N <- seq.int(1,25)
library(operator.tools)
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
代码工作没有错误消息。