简体   繁体   English

R if else声明问题

[英]R if else statement issue

When I run this very simple code 当我运行这个非常简单的代码时

c <- seq(0, 1, length.out=4)
x <- 0.5
for (l in 1:length(c)) {
    if (l>1 & x>c[l-1] & x<c[l]) {
        print(x)
    }
}

I have the following error in the console 我在控制台中有以下错误

Error in if ((l > 1 & x > c[l - 1] & x < c[l])) { : 
  argument is of length zero

The most interesting part is that if try to create a logical variable with the "if" condition, everything works fine: 最有趣的部分是,如果尝试使用“if”条件创建逻辑变量,一切正常:

> (l>1 & x>c[l-1] & x<c [l])
logical(0)

What am I doing wrong? 我究竟做错了什么?

That was a stupid mistake. 那是一个愚蠢的错误。 According to this replacing & with && would solve the issue for my case. 根据这个替换&&&将解决我的情况的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM