R问题循环遍历数组

时间:2017-06-02 13:07:07

标签: r loops

我试图循环一个数组,最后我将创建一个动力曲线,通过每次治疗的动物数量和两种治疗之间的平均差异来显示动力

[<-

我得到的错误是:

  

*tmp*中的错误(ng-style,i,j,q,值= 0.00490665200011608):
  下标超出范围

我的直觉是说我犯了一个简单而愚蠢的错误。不幸的是,这些错误可能需要数小时。希望有人能看到快速而简单的解决方法。

1 个答案:

答案 0 :(得分:0)

这里有各种各样的问题,但希望这有助于解决其中一些......

N=30                # number of maximum simulations per K
K=seq(10,30,1)      # maximum number of animals per group
ES=seq(1,2,0.1)     # mean difference compared to control

x=array(data=NA, 
        dim=c(N,length(K),length(ES)), 
        dimnames =list(paste("Sim",1:N, sep=""),
                       paste("Total Number of Animals=",min(K):max(K), sep=""),
                       ES))  # 3-dimensional matrix in which to store the values

#all OK to here
#in the next loop, popmeansum$V3 is not defined.  If it is a single value, then I suggest...
pms <- popmeansum$V3[1]

for (q in 1:length(ES)){ #keep q as integers so that you can use it for indexing
  for (j in 1:length(K)){ #to be consistent with your dimensions of x
    for (i in 1:N){
      controle<-rnorm(K[j],pms,1.490918)
      new<-rnorm(K[j],pms-ES[q],1.490918)
      fit<-t.test(controle, new, alternative ="greater") 
      x[i,j,q]<-fit$p.value
    }
  }
}

它不是最简单的代码,但它至少做了一些事情!

相关问题