Dear all, how can I convert an m x n incidence matrix into an m x m adjacency matrix or an n x n adjacency matrix? The current matrix contains binary data, hence the new matrix would contain counts of common occurrences. Thank you for your help. Phil
I think you can do this using the network package. Look at the as.network.matrix and as.matrix.network functions, for example. On 05/12/06, Philip Leifeld <Philip.Leifeld at uni-konstanz.de> wrote:> Dear all, > > how can I convert an m x n incidence matrix into an m x m adjacency > matrix or an n x n adjacency matrix? The current matrix contains binary > data, hence the new matrix would contain counts of common occurrences. > > Thank you for your help. > > Phil > > ______________________________________________ > 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. >-- ================================David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP
Thanks, David. I tried converting my rectangular matrix to a network object and then back to a square matrix. This works for a small artificial dataset (e.g. 2x5), but when I try this on my 1790x45 or transposed 45x1790 matrix, R keeps working for a couple of minutes and then crashes completely without having saved any data first. I assume something is wrong with my data? Cheers Philip David Barron wrote:> I think you can do this using the network package. Look at the > as.network.matrix and as.matrix.network functions, for example. > >> how can I convert an m x n incidence matrix into an m x m adjacency >> matrix or an n x n adjacency matrix? The current matrix contains binary >> data, hence the new matrix would contain counts of common occurrences.
Thanks, David. I tried converting my rectangular matrix to a network object and then back to a square matrix. This works for a small artificial dataset (e.g. 2x5), but when I try this on my 1790x45 or transposed 45x1790 matrix, R keeps working for a couple of minutes and then crashes completely without having saved any data first. I assume something is wrong with my data? Cheers Philip David Barron wrote: > I think you can do this using the network package. Look at the > as.network.matrix and as.matrix.network functions, for example. > >> how can I convert an m x n incidence matrix into an m x m adjacency >> matrix or an n x n adjacency matrix? The current matrix contains binary >> data, hence the new matrix would contain counts of common occurrences.
>>>>> "Phil" == Philip Leifeld <Philip.Leifeld at uni-konstanz.de> >>>>> on Tue, 05 Dec 2006 22:30:02 +0100 writes:Phil> Dear all, Phil> how can I convert an m x n incidence matrix into an m x m adjacency Phil> matrix or an n x n adjacency matrix? The current matrix contains binary Phil> data, hence the new matrix would contain counts of common occurrences. You have not given a self contained reproducible example which would even be useful in such a case.. But just to be sure that the solution is not as simple as I thought it was -- namely I thought, that for binary matrices, crossprod() and tcrossprod() would give what you'd want :> n <- 10 ; m <- 4 > set.seed(1) > M <- matrix(as.logical(rbinom(n*m, size=1, prob = 0.3)), n,m) > symnum(M)[1,] . . | . [2,] . . . . [3,] . . . . [4,] | . . . [5,] . | . | [6,] | . . . [7,] | | . | [8,] . | . . [9,] . . | | [10,] . | . .> print.table(A.1 <- tcrossprod(M), zero = ".")[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 . . . . . . . 1 . [2,] . . . . . . . . . . [3,] . . . . . . . . . . [4,] . . . 1 . 1 1 . . . [5,] . . . . 2 . 2 1 1 1 [6,] . . . 1 . 1 1 . . . [7,] . . . 1 2 1 3 1 1 1 [8,] . . . . 1 . 1 1 . 1 [9,] 1 . . . 1 . 1 . 2 . [10,] . . . . 1 . 1 1 . 1> print.table(A.2 <- crossprod(M), zero = ".")[,1] [,2] [,3] [,4] [1,] 3 1 . 1 [2,] 1 4 . 2 [3,] . . 2 1 [4,] 1 2 1 3>Phil> Thank you for your help. you're welcome (if it did help). Martin Maechler, ETH Zurich