Reading about R types I came across this answer where class()
is used inside of Map
. Now I wonder why class()
returns something else if used inside Map()
. See here:
# Using class() without Map()
> class(matrix())
[1] "matrix" "array"
# Using class() within Map()
Map(function(x){class(x)}, matrix())
[[1]]
[1] "logical"
Why is this happening?
EDIT
Inferred from comment and answer: To obtain the expected, i.e. same output, we need to use
Map(function(x){class(x)}, list(matrix()))
[[1]]
[1] "matrix" "array"
CodePudding user response:
You apply (map) all elements of an empty matrix to a function returning it's class. The class of an empty thing is NA
. A vector only containing NAs is of class logical in R.