在值可以为null或字符串时编写IF语句

发布于 2025-02-11 23:22:29 字数 224 浏览 2 评论 0原文

我有一个可以是null或字符串的值。第二个测试失败,因为a具有长度0。 如何编写处理两种情况的 ?

例子:

a <- NULL
    
if (is.null(a) | a=="something else then null") {
  print("test")
} 

I have a value that can be NULL or a string. The second test fails because a is of length 0.
How do I write an if-statement that handles both cases?

Example:

a <- NULL
    
if (is.null(a) | a=="something else then null") {
  print("test")
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2025-02-18 23:22:29

is.null(a)|| a ==“然后null”

(同样,在这种情况下,请使用&amp;&amp;,而不是&amp;

说明:当您使用时|,一一检查所有条件,然后应用“或”断言。这意味着,如果其中一个条件是无效的,那么在您的情况下会出现错误。

当您使用||时,每个条件检查后都会检查“或”断言。在您的情况下,is.null(a)true。这意味着is.null(a)”或“任何其他条件也将是true,因此无需检查其他条件。

这是基于我对它的工作方式的理解,而不是来自文档的定义。我希望它更清楚。

更多信息:&amp之间有什么区别; &&amp;&amp;,|和||在r?

is.null(a) || a=="something else then null"

(similarly, use && instead of & in a situation like this)

Explanation: when you use |, all conditions are checked one by one and then the "or" assertion is applied. This means that if one of the conditions is invalid, then there is an error, like in your case.

When you use ||, the "or" assertion is checked after each condition check. In your case, is.null(a) is TRUE. This means that is.null(a) "or" any other condition will also be TRUE, so there's no need to check the other condition.

This is based on my understanding of how it works, it's not a definition coming from the docs. I hope it's clearer.

More info: What's the differences between & and &&, | and || in R?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文