# [BioC] Re: [R] A co-occurrence matrix

Sean Davis sdavis2 at mail.nih.gov
Tue Nov 11 13:04:15 MET 2003

```Sorry about the misunderstanding.  If you form matrix m as I wrote before,
then I think the following will give you the matrix you are interested in:

> obj <- c(1,2,3,4,5)
> group <- c(1,2,1,1,3)
> m <- matrix(rep(0,3*5),nrow=5,ncol=3)
> m
[,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0
[5,]    0    0    0
> for (i in 1:5) {m[obj[i],group[i]]=1}
> m
[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    1    0    0
[4,]    1    0    0
[5,]    0    0    1
> c <- crossprod(t(m),t(m))
> c
[,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

Note that crossprod(m,m) will give you counts of objects within groups:

> c <- crossprod(m,m)
> c
[,1] [,2] [,3]
[1,]    3    0    0
[2,]    0    1    0
[3,]    0    0    1

Hope this helps.
Sean

On 11/11/03 6:37 AM, "Alexey Shipunov" <a.shipunov at rbgkew.org.uk> wrote:
>> The object indices are in rows, and group indices in columns.
> ... but I actually mean that columns and
> rows are _both_ represent objects, not groups. Thus, if two objects
> (say, 2 and 3) share the same group, the cells [2, 3] and [3, 2]
> have value "1".
>
> Best wishes,
>
>
> =================================
> 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
>

```