!is.na()结果求和的行为 [英] Behavior of summing !is.na() results

查看:99
本文介绍了!is.na()结果求和的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么第一行返回TRUE,而第三行返回1?我希望这两行都返回1.第三行中多余的两个括号的确切含义是什么?

Why does the first line return TRUE, and the third line returns 1? I would expect both lines to return 1. What is the exact meaning of those extra two parentheses in the third line?

!is.na(5) + !is.na(NA)
# TRUE
(!is.na(5)) + (!is.na(NA))
# 1

应多次检查.最初的问题出在!is.na()上,以为它是为is.na()复制的.但这不是:)

edit: should check these multiple times. The original problem was with !is.na(), thought it replicated for is.na(). But it didn't :)

推荐答案

!在R中具有怪异的,违反直觉的优先级.

! has a weird, counter-intuitive precedence in R.

您的第一个代码等同于

!(is.na(5) + !is.na(NA))

也就是说,!的优先级低于+.

That is, ! has lower precedence than +.

这篇关于!is.na()结果求和的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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