Dear R experts, I have a matrix (from some sort of classification) like this: object group [1,] 1 1 [2,] 2 2 [3,] 3 1 [4,] 4 1 [5,] 5 3 And I need something like this: [,1] [,2] [,3] [,4] [,5] [1,] 1 0 1 1 0 [2,] 0 1 0 0 0 [3,] 1 0 1 1 0 [4,] 1 0 1 1 0 [5,] 0 0 0 0 1 where all zeros mean that these objects are not in same group, and vice versa. Is there a relatively simple way to construct co- uccurence matrices of type shown above? Any help would be appreciated, ================================Dr. Alexey B. Shipunov Section of Molecular Systematics Jodrell Laboratory Royal Botanic Gardens, Kew, Richmond, Surrey, TW9 3DS, U.K. e-mail: a.shipunov at rbgkew.org.uk
Dear Alexey, You can use outer(), as follows: > group <- c(1, 2, 1, 1, 3) > matrix(as.numeric(outer(group, group, "==")), 5, 5) [,1] [,2] [,3] [,4] [,5] [1,] 1 0 1 1 0 [2,] 0 1 0 0 0 [3,] 1 0 1 1 0 [4,] 1 0 1 1 0 [5,] 0 0 0 0 1 > If a logical result will do, the expression is even simpler: > outer(group, group, "==") [,1] [,2] [,3] [,4] [,5] [1,] TRUE FALSE TRUE TRUE FALSE [2,] FALSE TRUE FALSE FALSE FALSE [3,] TRUE FALSE TRUE TRUE FALSE [4,] TRUE FALSE TRUE TRUE FALSE [5,] FALSE FALSE FALSE FALSE TRUE I hope that this helps, John At 10:49 AM 11/11/2003 +0000, Alexey Shipunov wrote:>Dear R experts, > >I have a matrix (from some sort of >classification) like this: > > object group > [1,] 1 1 > [2,] 2 2 > [3,] 3 1 > [4,] 4 1 > [5,] 5 3 > >And I need something like this: > > [,1] [,2] [,3] [,4] [,5] > [1,] 1 0 1 1 0 > [2,] 0 1 0 0 0 > [3,] 1 0 1 1 0 > [4,] 1 0 1 1 0 > [5,] 0 0 0 0 1 > >where all zeros mean that these objects are not >in same group, and vice versa. > >Is there a relatively simple way to construct co- >uccurence matrices of type shown above? > >Any help would be appreciated, >----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
You can try the following commands, which I have not tested extensively, m <- data.frame( object=c(1,2,3,4,5), group=c(1,2,1,1,3) ) tab <- table(m) out <- tab %*% t(tab) The above is OK if every object belongs to one group only. But if it does not, say as in "m2 <- rbind(m, c(1,3))", the values above 1 can occur on the diagonal indicating the number of membership that object has. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch on behalf of Alexey Shipunov Sent: Tue 11/11/2003 18:49 To: R-help at stat.math.ethz.ch Cc: Subject: [R] A co-occurrence matrix Dear R experts, I have a matrix (from some sort of classification) like this: object group [1,] 1 1 [2,] 2 2 [3,] 3 1 [4,] 4 1 [5,] 5 3 And I need something like this: [,1] [,2] [,3] [,4] [,5] [1,] 1 0 1 1 0 [2,] 0 1 0 0 0 [3,] 1 0 1 1 0 [4,] 1 0 1 1 0 [5,] 0 0 0 0 1 where all zeros mean that these objects are not in same group, and vice versa. Is there a relatively simple way to construct co- uccurence matrices of type shown above? Any help would be appreciated, ================================ Dr. Alexey B. Shipunov Section of Molecular Systematics Jodrell Laboratory Royal Botanic Gardens, Kew, Richmond, Surrey, TW9 3DS, U.K. e-mail: a.shipunov at rbgkew.org.uk ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help <https://www.stat.math.ethz.ch/mailman/listinfo/r-help>
Does the following help?> group <- c(1, 2, 1, 1, 3) > outer(group, group, "==")[,1] [,2] [,3] [,4] [,5] [1,] TRUE FALSE TRUE TRUE FALSE [2,] FALSE TRUE FALSE FALSE FALSE [3,] TRUE FALSE TRUE TRUE FALSE [4,] TRUE FALSE TRUE TRUE FALSE [5,] FALSE FALSE FALSE FALSE TRUE> outer(group, group, "==") + 0 # Turn it into a numeric matrix.[,1] [,2] [,3] [,4] [,5] [1,] 1 0 1 1 0 [2,] 0 1 0 0 0 [3,] 1 0 1 1 0 [4,] 1 0 1 1 0 [5,] 0 0 0 0 1 Andy> -----Original Message----- > From: Alexey Shipunov [mailto:a.shipunov at rbgkew.org.uk] > Sent: Tuesday, November 11, 2003 5:50 AM > To: R-help at stat.math.ethz.ch > Subject: [R] A co-occurrence matrix > > > Dear R experts, > > I have a matrix (from some sort of > classification) like this: > > object group > [1,] 1 1 > [2,] 2 2 > [3,] 3 1 > [4,] 4 1 > [5,] 5 3 > > And I need something like this: > > [,1] [,2] [,3] [,4] [,5] > [1,] 1 0 1 1 0 > [2,] 0 1 0 0 0 > [3,] 1 0 1 1 0 > [4,] 1 0 1 1 0 > [5,] 0 0 0 0 1 > > where all zeros mean that these objects are not > in same group, and vice versa. > > Is there a relatively simple way to construct co- > uccurence matrices of type shown above? > > Any help would be appreciated, > > > ================================> Dr. Alexey B. Shipunov > Section of Molecular Systematics > Jodrell Laboratory > Royal Botanic Gardens, Kew, > Richmond, Surrey, TW9 3DS, U.K. > e-mail: a.shipunov at rbgkew.org.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >
> [,1] [,2] [,3] [,4] [,5] > [1,] 1 0 1 1 0 > [2,] 0 1 0 0 0 > [3,] 1 0 1 1 0 > [4,] 1 0 1 1 0 > [5,] 0 0 0 0 1 > > where all zeros mean that these objects are not > in same group, and vice versa.This is a block diagonal matrix of 1s and 0s with rows and cols permuted. A block diagonal matrix can be set up with: blockdiag <- function (v) { n <- sum(v) onezero <- function(k) { c(rep(c(rep(1,k), rep(0,n-k)), k), rep(0,k)) } array(unlist(sapply(v, onezero))[1:(n*n)], c(n,n)) } John's answer just came in; it seems more elegant to me. Greetings Johannes