为什么 1..99,999 == “1"..“99,999"?在 R 中,但 100,000 != “100,000"? [英] Why does 1..99,999 == "1".."99,999" in R, but 100,000 != "100,000"?

查看:20
本文介绍了为什么 1..99,999 == “1"..“99,999"?在 R 中,但 100,000 != “100,000"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制台中,继续尝试

> sum(sapply(1:99999, function(x) { x != as.character(x) }))
0

对于 1 到 99999 的所有值,"1" == 1, "2" == 2, ..., 99999 == "99999"TRUE.然而,

For all of values 1 through 99999, "1" == 1, "2" == 2, ..., 99999 == "99999" are TRUE. However,

> 100000 == "100000"
FALSE

为什么 R 会有这种古怪的行为,这是一个错误吗?例如,检查原子字符向量中的每个元素是否实际上都是数字的解决方法是什么?现在我正在尝试检查每个 x 是否有 x == as.numeric(x),但由于上述问题,在某些数据集上失败了!

Why does R have this quirky behavior, and is this a bug? What would be a workaround to, e.g., check if every element in an atomic character vector is in fact numeric? Right now I was trying to check whether x == as.numeric(x) for each x, but that fails on certain datasets due to the above problem!

推荐答案

看看as.character(100000).它的值不等于"100000"(你自己看看),R本质上只是告诉你.

Have a look at as.character(100000). Its value is not equal to "100000" (have a look for yourself), and R is essentially just telling you so.

as.character(100000)
# [1] "1e+05"

这里,来自 ?Comparison,是 R 将关系运算符应用于不同类型的值的规则:

Here, from ?Comparison, are R's rules for applying relational operators to values of different types:

如果两个参数是不同类型的原子向量,一个是强制到另一个的类型,(递减)顺序优先级是字符、复数、数字、整数、逻辑和生的.

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.

这些规则意味着当你测试 1=="1" 时,比如说,R 首先将 LHS 上的数值转换为字符串,然后测试字符串是否相等在 LHS 和 RHS 上.在某些情况下,它们会相等,但在其他情况下,它们不会.哪些情况会产生不等式将取决于 options("scipen")options("digits")

Those rules mean that when you test whether 1=="1", say, R first converts the numeric value on the LHS to a character string, and then tests for equality of the character strings on the LHS and RHS. In some cases those will be equal, but in other cases they will not. Which cases produce inequality will be dependent on the current settings of options("scipen") and options("digits")

因此,当您键入 100000=="100000" 时,就好像您实际上正在执行以下测试.(请注意,在内部,R 很可能/很可能确实使用了不同于 as.character() 的东西来执行转换):

So, when you type 100000=="100000", it is as if you were actually performing the following test. (Note that internally, R may well/probably does use something different than as.character() to perform the conversion):

as.character(100000)=="100000"
# [1] FALSE

这篇关于为什么 1..99,999 == “1"..“99,999"?在 R 中,但 100,000 != “100,000"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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