R中的if()语句或while()循环

时间:2017-07-24 09:24:09

标签: r loops if-statement while-loop

x=matrix(0)
test <- vector(mode="numeric", length=196)
for(i in 1:196){
    x=c[c(1:(200+i*50)),]
    ts=BiCopGofTest(x[,1], x[,2], 1, par =  0.6 ,method="white",max.df = 30, B = 0, obj = NULL)
    test[i]=ts$statistic}
plot(test, type='l')

我有矩阵c,它有10,000行和2列。我拿了前200行矩阵c,我计算了测试的值。每次我将行数增加50并且我正在计算测试。这就是上面代码中的内容。

我现在要做的是以下内容。我想重复相同的过程,但是当测试大于7.81时,我想停止并返回数组x。我想稍后使用该数组,因此存储它非常重要。我应该使用IF语句还是WHILE循环?任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

请注意,通过执行

x=c[c(1:(200+i*50)),]
在你的循环中,你覆盖了x,你就失去了原始矩阵。此外,您的子集语句并不正确。您应该创建一个新变量,它保存循环中的行子集。您可以执行以下操作:

x=matrix(runif(10000*2,0,1),10000,2)
test <- vector(mode="numeric", length=196)

i=0

while(max(test)<7.81 & 200+i*50<=nrow(x)  )
{
     y = x[seq(1,200+i*50),] # y contains the first 200+i*50 rows of x
     ts= runif(1,1,7.9) # random test statistic, enter yours here.
     test[i]= sum(ts)
     i=i+1
}
plot(test, type='l')

这将继续循环,直到:

  • x
  • 中没有其他行
  • 检验统计量大于7.81

请注意,我没有包含BiCopGofTest的软件包,所以我只使用runif作为我的测试统计信息;)

希望这有帮助!

相关问题