错误..缺少值,其中TRUE / FALSE需要 [英] Error .. missing value where TRUE/FALSE needed

查看:188
本文介绍了错误..缺少值,其中TRUE / FALSE需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图运行这个代码(在这里下面),我得到了这个消息错误在if(temp [ii] == 0){:缺少值,其中TRUE / FALSE需要...

  temp = c(2.15,3.5,0,0,1.24,5.42,6.87)
tm = length(temp )
for(i in 1:tm){
if(temp [i] == 0){
counter3 = 1
last = temp [i - 1]
$ for(ii in i + 1:tm){
if(temp [ii] == 0){
counter3 = counter3 + 1
}
if(temp [ ii]!= 0){
nxt = temp [i + counter3]
}
}
}
}


解决方案

您的问题是 temp [ii] 是返回 NA ,因为 ii 超出范围:

  ii = i + 1:tm#您对ii 
ii = 1的声明:tm + 1:tm #Evaluates to
pre>

所以 ii 一定会大于 tm (因此 length(temp))。



为了更好地理解/调试循环,请考虑只打印索引:

$ b (i在1:tm)
{
print(i)
(ii在i + 1中)
$ b

  :tm)
print(ii)
}


I have been trying to run this code (below here) and I have gotten that message "Error in if (temp[ii] == 0) { : missing value where TRUE/FALSE needed"...

temp = c(2.15, 3.5, 0, 0, 0, 1.24, 5.42, 6.87)
tm = length(temp)
for (i in 1:tm){
    if (temp[i] == 0) {
        counter3 = 1
        last = temp[i - 1]
        for (ii in i + 1:tm){
            if (temp[ii] == 0) {
                counter3 = counter3 + 1
            }
            if (temp[ii] != 0) {
                nxt = temp[i + counter3]
            }
        }
    }
}

解决方案

Your problem is that temp[ii] is returning NA because ii goes out of bounds:

ii = i + 1:tm     #Your declaration for ii
ii = 1:tm + 1:tm  #Evaluates to

So ii will definitely be larger than tm (and therefore length(temp) at some point.

In order to better understand/debug for loops, consider printing just the indices:

for(i in 1:tm)
{
    print(i)
    for(ii in i + 1:tm)
        print(ii)
}

这篇关于错误..缺少值,其中TRUE / FALSE需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆