为什么 class() return 在 Map() 中不一样?
Why does class() return not the same inside Map()?
阅读有关 R 类型的文章时,我遇到了 this 答案,其中 class()
在 Map
中使用。现在我想知道为什么 class()
returns 如果在 Map()
中使用其他东西。看这里:
# Using class() without Map()
> class(matrix())
[1] "matrix" "array"
# Using class() within Map()
Map(function(x){class(x)}, matrix())
[[1]]
[1] "logical"
为什么会这样?
编辑
从评论和答案推断:要获得预期的,即相同的输出,我们需要使用
Map(function(x){class(x)}, list(matrix()))
[[1]]
[1] "matrix" "array"
您将一个空矩阵的所有元素应用(映射)到一个返回它 class 的函数。一个空的东西的class是NA
。仅包含 NA 的向量在 R 中是 class 逻辑的。
阅读有关 R 类型的文章时,我遇到了 this 答案,其中 class()
在 Map
中使用。现在我想知道为什么 class()
returns 如果在 Map()
中使用其他东西。看这里:
# Using class() without Map()
> class(matrix())
[1] "matrix" "array"
# Using class() within Map()
Map(function(x){class(x)}, matrix())
[[1]]
[1] "logical"
为什么会这样?
编辑
从评论和答案推断:要获得预期的,即相同的输出,我们需要使用
Map(function(x){class(x)}, list(matrix()))
[[1]]
[1] "matrix" "array"
您将一个空矩阵的所有元素应用(映射)到一个返回它 class 的函数。一个空的东西的class是NA
。仅包含 NA 的向量在 R 中是 class 逻辑的。