一些元素在使用 %in% seq() 时不会显示为序列的成员。

huangapple go评论64阅读模式
英文:

Some elements dont show up as members of a sequence in R when using %in% seq()

问题

我试图检查值是否存在于R中使用%in%运算符的一系列数字中。该序列是使用seq()函数生成的,步长为0.01,范围从0.50到1.10。

这里是我尝试过的不同输入:

.55 %in% seq(.50, 1.1, by = .01)
.57 %in% seq(.50, 1.1, by = .01)

".55" %in% seq(.50, 1.1, by = .01)
".57" %in% seq(.50, 1.1, by = .01)

"0.55" %in% seq(.50, 1.1, by = .01)
"0.57" %in% seq(.50, 1.1, by = .01)

0.55 %in% seq(.50, 1.1, by = .01)
0.57 %in% seq(.50, 1.1, by = .01)

然而,对这些输入的输出令人困惑和意外:

[1] TRUE
[1] FALSE

[1] FALSE
[1] FALSE

[1] TRUE
[1] TRUE

[1] TRUE
[1] FALSE
  1. 有人知道为什么.55被识别在序列中,但.57不被识别吗?

  2. 为什么当我们加一个前导零并加上引号时返回TRUE?

  3. 为什么没有前导零的引号返回FALSE?

英文:

I am trying to check if values are present in a sequence of numbers using the %in% operator in R. The sequence is generated using the seq() function with a step of 0.01, ranging from 0.50 to 1.10.

Here are the different inputs I tried:

.55 %in% seq(.50, 1.1, by = .01)
.57 %in% seq(.50, 1.1, by = .01)

".55" %in% seq(.50, 1.1, by = .01)
".57" %in% seq(.50, 1.1, by = .01)

"0.55" %in% seq(.50, 1.1, by = .01)
"0.57" %in% seq(.50, 1.1, by = .01)

0.55 %in% seq(.50, 1.1, by = .01)
0.57 %in% seq(.50, 1.1, by = .01)

However, the outputs for these inputs are confusing and unexpected:

[1] TRUE
[1] FALSE

[1] FALSE
[1] FALSE

[1] TRUE
[1] TRUE

[1] TRUE
[1] FALSE
  1. Does anyone know why .55 is recognized in the sequence, but .57 isn't?

  2. How come when we add a leading zero and put quotes it returns TRUE?

  3. How come quotes without a leading zero returns FALSE?

答案1

得分: 3

关于你第一个问题的答案是,将浮点数进行精确相等比较是不安全的 - 请参见“为什么这些数字不相等?”

对于你第二个问题的答案是,seq 中的数字被转换为字符进行比较,序列的字符输出会显示在控制台上,如果我们执行:

as.character(seq(.50, 1.1, by = .01))
#>  [1] "0.5"  "0.51" "0.52" "0.53" "0.54" "0.55" "0.56" "0.57" "0.58" "0.59" "0.6" 
#> [12] "0.61" "0.62" "0.63" "0.64" "0.65" "0.66" "0.67" "0.68" "0.69" "0.7"  "0.71"
#> [23] "0.72" "0.73" "0.74" "0.75" "0.76" "0.77" "0.78" "0.79" "0.8"  "0.81" "0.82"
#> [34] "0.83" "0.84" "0.85" "0.86" "0.87" "0.88" "0.89" "0.9"  "0.91" "0.92" "0.93"
#> [45] "0.94" "0.95" "0.96" "0.97" "0.98" "0.99" "1"    "1.01" "1.02" "1.03" "1.04"
#> [56] "1.05" "1.06" "1.07" "1.08" "1.09" "1.1" 

注意这里都包含字符串 "0.55""0.57"

对于你第三个问题的答案很简单,字符串 ".55" 和 ".57" 不会 出现在上面的结果中。

英文:

The answer to your first question is that it is not safe to compare floating point numbers for exact equality - see "Why are these numbers not equal?"

The answer to your second question is that the numbers in seq are converted to characters for the comparison, and the character output of the sequence is what is shown in the console if we do:

as.character(seq(.50, 1.1, by = .01))
#>  [1] "0.5"  "0.51" "0.52" "0.53" "0.54" "0.55" "0.56" "0.57" "0.58" "0.59" "0.6" 
#> [12] "0.61" "0.62" "0.63" "0.64" "0.65" "0.66" "0.67" "0.68" "0.69" "0.7"  "0.71"
#> [23] "0.72" "0.73" "0.74" "0.75" "0.76" "0.77" "0.78" "0.79" "0.8"  "0.81" "0.82"
#> [34] "0.83" "0.84" "0.85" "0.86" "0.87" "0.88" "0.89" "0.9"  "0.91" "0.92" "0.93"
#> [45] "0.94" "0.95" "0.96" "0.97" "0.98" "0.99" "1"    "1.01" "1.02" "1.03" "1.04"
#> [56] "1.05" "1.06" "1.07" "1.08" "1.09" "1.1" 

Note that the strings "0.55" and "0.57" are both present here.

The answer to your third question is simply that the strings ".55" and ".57" do not appear in the above result.

huangapple
  • 本文由 发表于 2023年4月20日 04:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定