I’d like to know the reason why the following does not work on the matrix structure I have posted here (I’ve used the dput command).
When I try running:
apply(mymatrix, 2, sum)
I get:
Error in FUN(newX[, i], ...) : invalid 'type' (list) of argument
However, when I check to make sure it’s a matrix I get the following:
is.matrix(mymatrix)
[1] TRUE
I realize that I can get around this problem by unlisting the data into a temp variable and then just recreating the matrix, but I’m curious why this is happening.
?is.matrixsays:Your object is a list with a dim attribute. A list is a type of vector (even though it is not an atomic type, which is what most people think of as vectors), so
is.matrixreturnsTRUE. For example:To convert
mymatrixto an atomic matrix, you need to do something like this: