误差在sample.int(长度(X),大小,替换,概率):无效第一个参数

问题描述 投票:3回答:1

我运行下面的代码行,面向上述错误。任何想法如何解决这一问题 ?

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
}
r random sample
1个回答
2
投票

如你想让它实际工作。你可以看到它,如果你插入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代码工作没有错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.