1

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"
LulY
  • 976
  • 1
  • 9
  • 24
  • The documentation is pretty clear. `Map` is just a wrapper for `mapply`. `mapply` accepts vectors or lists as input for the ellipses argument. Thus, the dimension attribute of the matrix is not preserved. You are seeing the same result as from `m <- matrix(); dim(m) <- NULL; class(m)`. – Roland Feb 08 '22 at 09:10

1 Answers1

2

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.

danlooo
  • 10,067
  • 2
  • 8
  • 22