Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R apply statement does not work with a matrix

Tags:

r

matrix

sum

apply

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.

like image 951
user1357015 Avatar asked Dec 27 '22 17:12

user1357015


2 Answers

?is.matrix says:

'is.matrix' returns 'TRUE' if 'x' is a vector and has a '"dim"' attribute of length 2) and 'FALSE' otherwise.

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.matrix returns TRUE. For example:

> l <- as.list(1:10)
> dim(l) <- c(10,1)
> is.matrix(l)
[1] TRUE

To convert mymatrix to an atomic matrix, you need to do something like this:

mymatrix2 <- unlist(mymatrix, use.names=FALSE)
dim(mymatrix2) <- dim(mymatrix)
# now your apply call will work
apply(mymatrix2, 2, sum)
# but you should really use (if you're really just summing columns)
colSums(mymatrix2)
like image 106
Joshua Ulrich Avatar answered Dec 29 '22 05:12

Joshua Ulrich


The elements of your matrix are not numeric, instead they are list, to see this you can do:

apply(m,2, class) # here m is your matrix

So if you want the column sum you have to 'coerce' them to be numeric and then apply colSums which is a shortcut for apply(x, 2, sum)

colSums(apply(m, 2, as.numeric)) # this will give you the sum you want.
like image 45
Jilber Urbina Avatar answered Dec 29 '22 06:12

Jilber Urbina