R: `match` 函数在处理列表时行为不一致。

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

R: Inconsistent behavior in `match` with lists

问题

match 在处理列表对象时给出不一致的结果:

match(list(c(1, 2)), list(c(1L, 2L)))
# [1] NA
match(list(c(1, 3)), list(c(1L, 3L)))
# [1] 1

match 内部发生了什么导致了这种情况呢?

问题不仅仅是因为 integernumeric 之间的差异:

match(c(1, 2), c(1L, 2L))
# [1] 1 2
match(c(1, 3), c(1L, 3L))
# [1] 1 2

一些额外的示例。这种行为似乎只在连续值中出现:

match(list(c(100, 101, 102)), list(c(100L, 101L, 102L)))
# [1] NA
match(list(c(100, 102, 101)), list(c(100L, 102L, 101L)))
# [1] 1
match(list(c(2, 1)), list(c(2L, 1L)))
# [1] NA
英文:

match is giving inconsistent results when working with list objects:

match(list(c(1, 2)), list(c(1L, 2L)))
#> [1] NA
match(list(c(1, 3)), list(c(1L, 3L)))
#> [1] 1

What is going on under the hood in match to cause this?

It's not simply the difference between integer and numeric:

match(c(1, 2), c(1L, 2L))
#> [1] 1 2
match(c(1, 3), c(1L, 3L))
#> [1] 1 2

Some additional examples. The behavior seems to surface only with sequential values:

match(list(c(100, 101, 102)), list(c(100L, 101L, 102L)))
#> [1] NA
match(list(c(100, 102, 101)), list(c(100L, 102L, 101L)))
#> [1] 1
match(list(c(2, 1)), list(c(2L, 1L)))
#> [1] NA

答案1

得分: 4

以下是您要翻译的内容:

"原因是1,2是连续的整数

?match的详细部分中,它提到:

> 因子、原始向量和列表被转换为字符向量,然后x和table在匹配之前被强制转换为一个常见类型(R中两种类型中的后者的顺序,逻辑 < 整数 < 数值 < 复杂 < 字符)。如果incomparables的长度为正,它将被强制转换为常见类型。

对于连续的整数数值,它们被解析为起始数字:结束数字

as.character(list(c(1L,2L)))
[1] &quot;1:2&quot;

as.character(list(c(100L, 101L, 102L)))
[1] &quot;100:102&quot;

而另一种情况是:

as.character(list(c(1L,3L)))
[1] &quot;c(1, 3)&quot;

as.character(list(c(1,2)))
[1] &quot;c(1, 2)&quot;
英文:

The reason is 1,2 are consecutive integers.

In the detail section in ?match, it mentions:

> Factors, raw vectors and lists are converted to character vectors, and then x and table are coerced to a common type (the later of the two types in R's ordering, logical < integer < numeric < complex < character) before matching. If incomparables has positive length it is coerced to the common type.

For consecutive integer numbers, they are parsed as starting number: ending number

as.character(list(c(1L,2L)))
[1] &quot;1:2&quot;

as.character(list(c(100L, 101L, 102L)))
[1] &quot;100:102&quot;

Where as the other:

as.character(list(c(1L,3L)))
[1] &quot;c(1, 3)&quot;

as.character(list(c(1,2)))
[1] &quot;c(1, 2)&quot;

huangapple
  • 本文由 发表于 2023年8月5日 04:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838943.html
匿名

发表评论

匿名网友

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

确定