我的for循环遇到了错误。代码如下:
#finding IDs with >5% replicate variance
#initialize vectors
LS1repvariance = NULL
anomalylist = NULL
#open for loop iterating from 1 to end of dataset
for (i in 1:1523){
#call replicates, which start off as characters
charrep1 = widesubdat[i,2]
charrep2 = widesubdat[i,11]
#convert to numeric
rep1 = as.numeric(charrep1)
rep2 = as.numeric(charrep2)
#calculation
repvariance = (rep1-rep2)/((rep1+rep2)/2)*100
#if loop for anomalous replicates
if (abs(repvariance)>=5)
anomalylist[i]=widesubdat[i,0]
}
我得到的错误是
if(abs(repvariance)> = 5)anomalylist [i] = widesubdat [i,0]中的错误 :缺少需要TRUE / FALSE的值
我认为错误在迭代中,因为它将i定义为336L,并且它没有正确调用charrep,但我不知道为什么。我已经完成了python中的循环,但从未在R中完成,但所有for循环帮助页面似乎都具有相同的结构。我可以在for循环之外运行的所有行测试都没问题。
我已经读过,如果陈述也需要大括号,但IDLE表示意外" {"当我使用它们时。
答案 0 :(得分:0)
你也可以放弃循环
pick <- abs(200*(widesubdat[,2]-widesubdat[,11])/(widesubdat[,2]+widesubdat[,11]))>=5
anomalylist <- widesubdat[,1] # Note the comment above with index 0
anomalylist[!pick] <- NA