如果loop-argument的长度为零,则出错

时间:2013-12-16 10:53:27

标签: r

我有以下简单的代码,我面对以下错误,有人可以帮我搞清楚吗?它基本上应该使用两个向量a和b作为输入来计算得分:

Error in if (b[i] == 1) { : argument is of length zero

我的代码是:

require("Matrix")


# input vectors 
a<-c(0.01,0.02,0.09,0.81,0.54,0.04,0.05,0.11,0.44,0.08,0.03,0.06,0.07,0.22,0.21,0.34,0.77,0.89,0.45,0.13,0.32,0.42,0.21,0.73,0.66,0.88)

b<-c(0,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0)

#order the singificance vector together with pathway vector with increasing P-value
sort(a, decreasing=F)

b[order(a,decreasing =F)]

GSEA<-function(a,b){
m=length(a)
l=nnzero(b)
score<-c()

for (i in a){
  if(b[i]==1){
  score= + (m-l)
}else{score=score-l}

}

}
total<-c()
total.x<-c(1:m)
total.y<-score

1 个答案:

答案 0 :(得分:2)

当您说if(b[i]==1)时,您尝试使用非整数数值作为提取索引。

您可能需要for(i in seq_along(a))而不是for(i in a)

例如见:

> if((1:3)[.2]==1) 'hello world'
Error in if ((1:3)[0.2] == 1) "hello world" : argument is of length zero
相关问题