Good morning to you all, I have a problem with a set of matrices that I want to compare. I want to see the similarity between them, and to be able to extract the differences between them. They have all the same number of columns and rows, and correspond presence absence data: for example: 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) 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. Thank you, Carlos -- Carlos GUERRA Gabinete de Sistemas de Informacao Geografica Escola Superior Agraria de Ponte de Lima Mosteiro de Refoios do Lima 4990-706 Ponte de Lima Tlm: +351 91 2407109 Tlf: +351 258 909779 Reclaim your Inbox...!!! http://www.mozilla.org/products/thunderbird/
hi Carlos, its not really clear what you're asking here. If all you want is to see what entries are the same and which are different between two matrices of the same dimensions, then this does it: #same m1==m2 #diff m1 != m2 If you want to extract the ones that are the same, indx <- m1==m2> indx[,1] [,2] [,3] [,4] [1,] TRUE TRUE FALSE TRUE [2,] TRUE TRUE TRUE FALSE [3,] FALSE TRUE FALSE TRUE> m1[indx]1 0 0 1 0 1 1 1 On 3/19/07, Carlos Guerra <carlosguerra at esa.ipvc.pt> wrote:> Good morning to you all, > > I have a problem with a set of matrices that I want to compare. > > I want to see the similarity between them, and to be able to extract the > differences between them. > > They have all the same number of columns and rows, and correspond > presence absence data: > > for example: > > 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) > > 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. > > Thank you, > > Carlos > > -- > Carlos GUERRA > > Gabinete de Sistemas de Informacao Geografica > Escola Superior Agraria de Ponte de Lima > Mosteiro de Refoios do Lima > 4990-706 Ponte de Lima > > Tlm: +351 91 2407109 > Tlf: +351 258 909779 > > Reclaim your Inbox...!!! > http://www.mozilla.org/products/thunderbird/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Matthew C Keller Postdoctoral Fellow Virginia Institute for Psychiatric and Behavioral Genetics
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
On 3/19/07, Sarah Goslee <sarah.goslee at gmail.com> wrote:> 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)I thought of something else. If you want similarity, you could always do m12.dist <- dist(cbind(m1, m2), "binary") and just look at the elements of the result that correspond to columns of m1 vs m2 (rather than m1 vs m2 or m2 vs m2). Sarah -- Sarah Goslee http://www.functionaldiversity.org
Let me see if I can explain my problem better, I have: m1 <- matrix(c(1,0,0,0,1,0,1,1,1,1,1,1), 3, 4) rownames(m1) <- c("station1", "station2", "station3") colnames(m1) <- c("A","B","C","D") m2 <- matrix(c(1,0,1,1,1,0,1,1,0,0,1,1), 3, 4) rownames(m2) <- c("station1", "station2", "station3") colnames(m2) <- c("A","B","C","D") ... and I want to: - find the correlation between the two matrices - for each station, extract the names of the columns that don't match in the two matrices Thanks for the previous comments, Carlos -- Carlos GUERRA Gabinete de Sistemas de Informacao Geografica Escola Superior Agraria de Ponte de Lima Mosteiro de Refoios do Lima 4990-706 Ponte de Lima Tlm: +351 91 2407109 Tlf: +351 258 909779 Reclaim your Inbox...!!! http://www.mozilla.org/products/thunderbird/