1
$\begingroup$

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?

$\endgroup$
0

2 Answers 2

5
$\begingroup$

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

$\endgroup$
0
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.