[R] A co-occurrence matrix
"Hüsing, Johannes"
johannes.huesing at medizin.uni-essen.de
Tue Nov 11 13:49:14 CET 2003
> [,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
More information about the R-help
mailing list