英文:
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
内部发生了什么导致了这种情况呢?
问题不仅仅是因为 integer
和 numeric
之间的差异:
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] "1:2"
as.character(list(c(100L, 101L, 102L)))
[1] "100:102"
而另一种情况是:
as.character(list(c(1L,3L)))
[1] "c(1, 3)"
as.character(list(c(1,2)))
[1] "c(1, 2)"
英文:
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] "1:2"
as.character(list(c(100L, 101L, 102L)))
[1] "100:102"
Where as the other:
as.character(list(c(1L,3L)))
[1] "c(1, 3)"
as.character(list(c(1,2)))
[1] "c(1, 2)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论