[R] matrix similarity comparison

Sarah Goslee sarah.goslee at gmail.com
Mon Mar 19 17:03:58 CET 2007


Hi Carlos,

> I want to see the similarity between them, and to be able to extract the
> differences between them.

You need to explain a bit more. Are you looking for number of elements in
common? How are your data set up? (eg species as columns and sites
as rows)

One way to get number of joint presences is this (although there are
certainly more elegant ways), assuming that columns are species

> m1 <- matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3,4)
> m2 <- matrix(c(1,0,1,0,1,0,0,1,0,1,0,1), 3,4)

> apply((m1 + m2), 1, function(x)sum(x == 2))
[1] 2 2 1

> I tried with the function cor2m() [package=edodist] but it didn't worked
> and my matrices are much bigger than the ones from the example.

"Didn't work"? That's a bit vague, but anyway this won't help, since
cor2m is intended for use with a matrix of environmental variables as
the second matrix, and not for binary data (the first matrix can be
binary). I doubt that correlations are really the measure you want
anyway - if they are, then you can use simply
cor(m1, m2)

Sarah


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list