0

I have several thousand distance matrices (converted from a matrix using as.dist()) and would like to compute the mean, sd, median etc for each matrix element.

To illustrate:

Matrix.A

1
7 1
5 2 1

Matrix.B

2  
3 4
1 1 3

etc...

For example, if I would like to have the sum of the individual elements I could do:

Sum.Matrix <- Matrix.A + Matrix.B

Sum.Matrix

3
10 5
6 3 4

But what if I have thousands of these matrices? And how can I compute not just the sum for each element but also means, sd etc? All matrices are stored in a single list.

roschu
  • 766
  • 1
  • 8
  • 17
  • 1
    How is your collection of matrices stored? Are they in a `list` or just scattered throughout your workspace? – nrussell May 31 '15 at 15:45
  • Sorry, I forgot: They are stored in a list; I will add this information to the original post. – roschu May 31 '15 at 15:49
  • 1
    Please see [how to create a great reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier to help you. – MrFlick May 31 '15 at 16:17

1 Answers1

2

Try

lst2 <- lapply(lst1, as.matrix)
dim1 <- sapply(lst2, dim)[,1]
l <- length(lst1)
ar1 <- array(unlist(lst2), dim=c(dim1, l)) 

as.dist(apply(ar1, 1:2, sum))
as.dist(apply(ar1, 1:2, mean))
as.dist(apply(ar1, 1:2, sd))

data

set.seed(24)
lst1 <- lapply(1:4, function(i) dist(sample(1:10,4, replace=TRUE)))
akrun
  • 874,273
  • 37
  • 540
  • 662