Re: [R] A co-occurrence matrix
0
0
Entering edit mode
@sean-davis-490
Last seen 4 months ago
United States
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@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@rbgkew.org.uk >
• 1.1k views
ADD COMMENT

Login before adding your answer.

Traffic: 548 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6