I have a matrix in R. When I run class(my.matrix)
it returns:
"matrix" "array"
Why do I get "array" when in fact I want a plain matrix? The same class
output is returned when running as.matrix(my.matrix))
.
Why is that?
class()
on a matrix will always return:
"matrix" "array"
Simple example:
> class(as.matrix(1))
[1] "matrix" "array"
This is because an array in R is a vector with multiple dimensions, and the matrix is a special type of array with exactly two dimensions, so every matrix is an array and this is what the class
command on a matrix is telling us here, see http://adv-r.had.co.nz/Data-structures.html
From the return value of the class
function, it looks like this already is a matrix. It just happens that it is considered by R to be both a matrix and an array.