【问题标题】:Why does "hello" > 0 return TRUE?为什么“你好”> 0 返回 TRUE?
【发布时间】:2013-06-10 18:11:09
【问题描述】:

试试看:

"hello" > 0

我尝试使用as.numeric("hello"),但它只是给了我NA。什么给了?

【问题讨论】:

  • 这可能不会在所有语言环境中返回 TRUE。这取决于字符 "0" 在排序顺序中的位置。
  • 字符按“词法”排序。这也会影响默认情况下如何处理回归函数和轴绘图标签中的对比。
  • 只是出于好奇,你期待什么?
  • NA 可能。有些东西会警告我我正在尝试将字符串与数字进行比较。

标签: r


【解决方案1】:

因为0 被强制转换为"0"。见help(">"):

 If the two arguments are atomic vectors of different types, one is
 coerced to the type of the other, the (decreasing) order of
 precedence being character, complex, numeric, integer, logical and
 raw.

【讨论】:

    猜你喜欢
    • 2015-06-21
    • 2011-05-04
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    相关资源
    最近更新 更多